# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1401: Pandigital squares

From New Scientist #2561, 22nd July 2006

Harry, Tom and I each found a four-digit perfect square and two three-digit perfect squares that between them used all the digits 0-9. No two solutions were identical. If I told you how many squares my solution had in common with each of the other two solutions you could deduce with certainty which squares formed my solution.

(1) Which squares formed my solution?

(2) Which square or squares (if any) did Harry’s and Tom’s solutions have in common?

[enigma1401]

### 3 responses to “Enigma 1401: Pandigital squares”

1. Jim Randell 17 August 2013 at 9:35 am
```from itertools import combinations
from collections import defaultdict
from enigma import irange, is_duplicate, printf

# 3-digit squares with distinct digits
s3 = list(s for s in (str(i * i) for i in irange(10, 31)) if not is_duplicate(s))

# 4-digit squares with distinct digits
s4 = list(s for s in (str(i * i) for i in irange(32, 99)) if not is_duplicate(s))

# collect pandigital triples (a, b, c)
ts = set()
for a in s4:
s = set(a)
for (b, c) in combinations((x for x in s3 if not s.intersection(x)), 2):
if set(b).intersection(c): continue

# record triples by the count of the intersections
r = defaultdict(list)
# choose triple for D
for D in ts:
ds = set(D)
# and two triples for T and H
for (T, H) in combinations(ts.difference([D]), 2):
# how many squares does D have in common with T and H
k = tuple(sorted((len(ds.intersection(T)), len(ds.intersection(H)))))
r[k].append((D, T, H))

# look for unique solutions
for (k, v) in r.items():
if len(set(t[0] for t in v)) != 1: continue
# output possible solutions
printf("D: {D}", D=' '.join(v[0][0]))
for (D, T, H) in v:
printf("  T&H: {TH} [T/H: {T} / {H}]", T=' '.join(T), H=' '.join(H), TH=' '.join(set(T).intersection(H)))
```

Solution: (1) Your squares are 9025, 361 and 784; (2) Harry and Tom’s solutions have 3025 and 784 in common.

2. Naim Uygun 17 August 2013 at 11:32 am

I observed the output of this program.
Then I deleted the same results to reduce the number of the solutions to 5.
If the number of the intersection with 3 solutions is one number, it will be 784
That is, my solution is 361,784,9025
Harry’s and Tom’s solutions are:
169, 784, 3025 and 196, 784, 3025

```count=0
for i in range(10,32):
a=i*i
for j in range(10,32):
b=j*j

for k in range(32,100):
c=k*k
s=str(a)+str(b)+str(c)
kume=set(s)
if len(kume) != 10: continue
count +=1
print(count,")",a,b,c)
```
• Jim Randell 17 August 2013 at 11:39 am

That’s a solution, but we can’t actually deduce the “non-common” squares for Harry and Tom. The three squares 169, 196 and 961 use the same digits and Harry and Tom must have picked two of them, but we don’t know which two.