### PWC 176

# PWC 176

## Challenge 1 (Permuted multiples)

*x*such that

*x*, 2

*x,*3

*x*, 4

*x*, 5

*x*and 6

*x*all have the same digits in common (not in the same order obviously) and no other digits. The answer is 142857.

This runs in around 16 seconds (so does my Raku script). By contrast, the Raku contribution from our host, Mohammad Anwar here runs much faster: around 2 seconds. His logic is similar, but the difference seems to be because he uses join("") to stringify the digits, instead of gist. I was able to get similar times by changing .gist to .join("").

And leaving out stringifying completely and directly comparing the digits arrays via eqv is even faster, around 1.8 seconds. Here is that as a one-liner:

raku -e '(1 .. 200_000).grep({$_.comb.sort eqv (6*$_).comb.sort}).grep({$_.comb.sort eqv (5*$_).comb.sort}).grep({$_.comb.sort eqv (4*$_).comb.sort}).grep({$_.comb.sort eqv (3*$_).comb.sort}).grep({$_.comb.sort eqv (2*$_).comb.sort}).head(1).say'

Another contributor, Flavio Poletti alias polettix (see his blog here) has correctly deduced that the candidate cannot be less than 123456, so one could start looping from there, which would make it even faster (around 0.6 seconds adapting the above one-liner).

Writing out the 5 steps explicitly is about as fast as an inner loop in terms of both writing time (with cut, paste, edit) and run time. Equally readable too, I would think.

My Perl 5 script runs in around 0.7 seconds while my Julia script runs in around 0.6 seconds.

## Challenge 2 (Reversible numbers)

For this challenge, we are asked to find numbers less than 100 such that the sum of the number and its reverse includes only odd digits (reverse as in for example 31 is the reverse of 13).

Again I started with Raku. For each number from 1 to 100, we calculate the sum of the number and its reverse (using 'flip'), then comb the result and then loop through the digits exiting the loop if we spot an even one.

This logic translates directly to Perl 5 and to Julia.

## Comments