# 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?

[enigma1675]

### 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)
e=math.sqrt(d)
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="" \