Enigmatic Code

Programming Enigma Puzzles

Enigma 1675: Reversing the square

From New Scientist #2841, 3rd December 2011 [link]

I have selected a 5-digit number, which is a perfect square. I have created another number by reversing the digits of my perfect square. Neither number has a leading zero. If I subtract the smaller of my numbers from the larger the answer is another perfect square.

Which 5-digit perfect square did I initially select?


4 responses to “Enigma 1675: Reversing the square

  1. Jim Randell 30 November 2011 at 9:32 pm

    Another Enigma that is easily solved in Python. Runtime: 30ms.

    from enigma import is_square
    for i in range(100, 317):
      a = i * i
      if a % 10 == 0: continue
      b = int(str(a)[::-1])
      d = abs(a - b)
      if not(is_square(d)): continue
      print(a, b, d)

    Solution: The initial 5 digit square was 39204.

  2. dave randell 2 January 2012 at 7:34 pm

    couldn’t get the square function to work in P3.1, so here’s my mod version.

    import math
    for i in range(100, 317):
      a = i*i
      if a % 10 == 0: continue
      b = int(str(a)[::-1])
      d = abs(a - b)
      if e-int(e): continue
      if a==b : continue
      print(a, b, d,e)
  3. jimrandell 3 January 2012 at 12:08 am

    The [[ square() ]] function (now renamed [[ is_square() ]]) is from my own library of useful functions for solving Enigma puzzles, you can get it at http://www.magwag.plus.com/jim/enigma.html (and I put some [sourcecode language="python"]...[/sourcecode] tags around your code to show the indents properly).

  4. Jim Randell 13 August 2016 at 8:21 am

    We can solve this problem using the general alphametic expression solver [[ SubstitutedExpression() ]] in the enigma.py library.

    We use the [[ --distinct="" ]] parameter to allow the same digit to be substituted for different letters.

    Here is the command and it’s output:

    % pypy -m enigma SubstitutedExpression \
        --distinct="" \
        --answer="ABCDE" \
        "is_square(ABCDE)" \
        "is_square(abs(ABCDE - EDCBA))"
    (is_square(ABCDE)) (is_square(abs(ABCDE - EDCBA)))
    (is_square(39204)) (is_square(abs(39204 - 40293))) / A=3 B=9 C=2 D=0 E=4 / 39204
    ABCDE = 39204 [1 solution]

    The command runs in 170ms.

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: