# Enigmatic Code

Programming Enigma Puzzles

## Enigma 335: Square up

From New Scientist #1483, 21st November 1985 [link]

My calculating nephew has been foxing me again. He entered a three-figure number on the calculator, showed it to me, and then squared the number but tried to trick me by showing me the square upside down.

I could soon see that there was something wrong because although I could see a number it had no digits in common with the number that had been squared — whereas I know that their first digits should have been the same. Further investigation would have shown that the original number and its inverted square had no factors larger than 1 in common.

Now, without a computer, and without hours of work, you should be able to tell me what I want to know. What was the original three-figure number? And what number did my nephew show me trying to kid me that it was the square?

[enigma335]

### One response to “Enigma 335: Square up”

1. Jim Randell 4 March 2016 at 6:23 am

Obviously I did use a computer, but not hours of time. This Python program runs in 44ms.

```from enigma import irange, split, nconcat, gcd, printf

# numbers which read as numbers when inverted on a 7-segment display
inverted = { 0: 0, 1: 1, 2: 2, 5: 5, 6: 9, 8: 8, 9: 6 }

# invert a number (sequence of digits)
def invert(s):
try:
return nconcat(inverted[d] for d in s[::-1])
except KeyError:
return None

# the three digit starting number
for n1 in irange(100, 999):
s1 = split(n1, int)
# and its square
n2 = n1 * n1
# both n1 and n2 start with the same digit
s2 = split(n2, int)
if s1[0] != s2[0]: continue
# n2 is invertible
n3 = invert(s2)
if n3 is None: continue
# but n1 and n3 have no digits in common, and are co-prime
s3 = split(n3, int)
if set(s1).intersection(s3) or gcd(n1, n3) > 1: continue

printf("n1={n1}, n2={n2}, n3={n3}")
```

Solution: The original 3-digit number was 953. The inverted square that the nephew showed was 602806.