DEV Community

Simon Green
Simon Green

Posted on

Big number, small numbers

Weekly Challenge 176

Challenge, My solutions

Task 1: Permuted Multiples

Task

Write a script to find the smallest positive integer x such that x, 2x, 3x, 4x, 5x and 6x are permuted multiples of each other.

My solution

This is one challenge where we could over-engineer it to make it faster. Given that the solution can be found in a fraction of a second, this won't be necessary.

For example after 1,666, the next possible solution is 100,000. 1667 × 6 will result in a five digit number which can't possibly be a permutation of the original number.

The easiest way to work out if a number is a permutation is simply to sort the numbers numerically. In Python, we can do this by ''.join(sorted(str(num))). While join '', sort split //, $num will do a similar thing in Perl.

Like a lot of the challenges, I have a counter that starts at one, and increments until we have a solution. For each number, we figure out if the number multiplied 2 to 6 are a permutation of the original number, and exit the inner loop if it isn't.

Finally when I do find a solution, I print it out, and exit the main() function.

Did you know? The solution (142,857) has its own Wikipedia page.

Examples

$ ./ch-1.py 
142857

$ ./ch-1.pl
142857
Enter fullscreen mode Exit fullscreen mode

Task 2: Reversible Numbers

Task

Write a script to find out all Reversible Numbers below 100. A number is said to be a reversible if sum of the number and its reverse had only odd digits.

My solution

This is relatively straight forward challenge. Count from 1 to 99, and add to a solutions list if the number is a reversible number.

As python doesn't have an easy way to reverse an integer, I convert it to a string and back to an integer with int(str(num)[::-1]). In Perl, this simply done with the reverse method. To see if the number is odd, I compare it with the regular expression ^[13579]+$.

I then print all the numbers in a solutions list.

Examples

$ ./ch-2.py 
10, 12, 14, 16, 18, 21, 23, 25, 27, 30, 32, 34, 36, 41, 43, 45, 50, 52, 54, 61, 63, 70, 72, 81, 90

$ ./ch-2.pl 
10, 12, 14, 16, 18, 21, 23, 25, 27, 30, 32, 34, 36, 41, 43, 45, 50, 52, 54, 61, 63, 70, 72, 81, 90
Enter fullscreen mode Exit fullscreen mode

Latest comments (0)