Enigmatic Code

Programming Enigma Puzzles

Enigma 1535: Back to front

From New Scientist #2698, 7th March 2009 [link]

Joe has found that just a few numbers have a rather unique property. Reverse the order of the digits, and the new number is a multiple of the original. For some specified numbers of digits (four or more) there are only two numbers with the property.

Joe gave Penny the task of finding the two 6-digit numbers.

What is the lower 6-digit number?



2 responses to “Enigma 1535: Back to front

  1. Jim Randell 26 June 2012 at 6:07 pm

    Here’s my original Perl code. It’s pretty straightforward and runs in 11ms.

    use strict;
    my ($A, $B);
    for $A (100_000..999_999) {
      $B = reverse $A;
      next unless $B > $A;
      next unless $B % $A == 0;
      printf "%d [%d / %d = %d]\n", $A, $B, $A, $B / $A;
      last; # we only want the smallest $A

    Solution: The lower number is 109989.

    If you leave the program to run to completion (by removing the last statement), you also find that 219978 is the other possible answer.

    • Jim Randell 26 June 2012 at 6:10 pm

      And here’s the same thing in Python. It runs in 51ms.

      from enigma import irange, printf
      for a in irange(100000, 999999):
        b = int(str(a)[::-1])
        (n, r) = divmod(b, a)
        if n < 2 or r > 0: continue
        printf("{a} [{b} / {a} = {n}]")
        break # if we only want the smallest

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: