PWC 188
PWC 188 Challenge 1 (Divisible pairs) We are given a list of integers, @list say, as well as a scalar integer $k. We are asked to count the number of pairs of elements of @list whose sum is divisible by $k. Thus, if $a and $b are elements of @list, we count ($a,$b) as an eligible pair if ($a+$b) is divisible by $k. This is a potential one-liner in Raku. But I settled for a one-line subroutine. Here is the key snippet: ( 0 .. @test-1) . combinations( 2 ) . grep ({@test[ $_ ] . sum %% $k}) . elems The task specification asks us to find pairs of indices of @list rather than pairs of elements of @list directly. I have followed this specification literally with Raku. Of course it makes no difference if we get the pairs from the list directly. I did this with my Perl 5 and Julia versions, both of which are derived from the Raku one. Julia key snippet: for i in collect ( combinations (list, 2 )) if ( sum (i) % k == 0 )