# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1562: Same unused digits

From New Scientist #2725, 12th September 2009 [link]

I chose two four-digit perfect squares (with no leading zero) that contained eight different digits. So did Harry, and so did Tom.

Our six chosen squares were all different, but the two digits that were unused were the same for all three of us. If you knew what those two digits were you would be able to deduce with certainty what the six squares that we chose were.

What were the largest and smallest of our six squares?

[enigma1562]

### One response to “Enigma 1562: Same unused digits”

1. jimrandell 22 February 2012 at 1:15 pm

The following Python program runs in 40ms.

```from itertools import combinations
from collections import defaultdict
from enigma import is_duplicate, printf

# find suitable four digit squares (as strings)
squares = []
for i in range(32, 100):
s = str(i * i)
if is_duplicate(s): continue
squares.append(s)

# organise the squares into pairs that use the same 8 digits
pairs = defaultdict(list)
for (s1, s2) in combinations(squares, 2):
if is_duplicate(s1 + s2): continue
d = ''.join(sorted(list(s1 + s2)))
pairs[d].append((s1, s2))

# choose 3 pairs of distinct squares that use the same 8 digits
distinct = defaultdict(list)
for (k, v) in pairs.items():
if len(v) < 3: continue
for (t, d, h) in combinations(v, 3):
if len(set(t).union(d, h)) != 6: continue
distinct[k].append((t, d, h))

# look for groupings where there is a unique set of six squares
for (k, v) in distinct.items():
if len(v) != 1: continue
print(k, v[0])
s = v[0][0] + v[0][1] + v[0][2]
printf("max={max} min={min}", max=max(s), min=min(s))
```

Solution: The largest square is 8649. The smallest square is 2809.