# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1162: Triangular or square

From New Scientist #2318, 24th November 2001 [link]

Harry and Tom each chose a four-digit number that was either a perfect square or a triangular number and told me its last two digits. I deduced that Harry’s number was one of exactly two four-digit perfect squares or one of exactly two four-digit triangular numbers, but that Tom’s number was one of exactly three four-digit perfect squares or one of exactly three four-digit triangular numbers.

Then they told me that the sum of the digits of Harry’s number was the same as the sum of the digits of Tom’s number.

What were (a) Harry’s number and (b) Tom’s number?

[enigma1162]

### 5 responses to “Enigma 1162: Triangular or square”

1. Jim Randell 20 June 2016 at 7:55 am

This Python program runs in 34ms.

```from collections import defaultdict
from enigma import irange, sqrt, trirt, tri, intc, intf, join, printf

# 4-digit squares
squares = defaultdict(list)
for i in irange(intc(sqrt(1000)), intf(sqrt(9999))):
s = str(i ** 2)
squares[s[-2:]].append(s)

# 4-digit triangular numbers
tris = defaultdict(list)
for i in irange(intc(trirt(1000)), intf(trirt(9999))):
s = str(tri(i))
tris[s[-2:]].append(s)

# digit sum (of a string)
def dsum(s):
return sum(int(d) for d in s)

# collect the possible values for harry and tom
(H, T) = (defaultdict(list), defaultdict(list))
for (k, ss) in squares.items():
ts = tris[k]
# for harry there are 2 possible squares and 2 possible tris
if len(ss) == len(ts) == 2:
for s in ss + ts:
H[dsum(s)].append(s)
# for tom there are 3 possible squares and 3 possible tris
if len(ss) == len(ts) == 3:
for s in ss + ts:
T[dsum(s)].append(s)

# find values with the same digit sum
for (k, h) in H.items():
t = T.get(k, None)
if t:
printf("sum={k} H={h} T={t}", h=join(h, sep=','), t=join(t, sep=','))
```

Solution: (a) Harry’s number was 5041. (b) Tom’s number was 1081.

2. Brian Gladman 20 June 2016 at 9:15 pm
```from collections import defaultdict
from itertools import product

# map four digit squares to their last two digits
dgts_to_sq = defaultdict(list)
for s in (x ** 2 for x in range(32, 100)):
dgts_to_sq[s % 100].append(s)

# map four digit triangular numbers to their last two digits
dgts_to_tr = defaultdict(list)
for t in (x * (x + 1) // 2 for x in range(45, 141)):
dgts_to_tr[t % 100].append(t)

# since both squares and triangular numbers are found with the
# same last two digits, these digits must occur in both lists
harry, tom = [], []
for dgts in dgts_to_sq.keys() & dgts_to_tr.keys():

# Harry's possible numbers are both from pairs, Tom's are
# both from triplets
ln = len(dgts_to_sq[dgts])
if ln in (2, 3) and len(dgts_to_tr[dgts]) == ln:
(harry if ln == 2 else tom).extend(dgts_to_sq[dgts] + dgts_to_tr[dgts])

# compute digit sums for integers
f = lambda x: sum(map(int, str(x)))

# now find numbers in Harry's and Tom's lists with the same digit sums
for h, t in (p for p in product(harry, tom) if f(p) == f(p)):
print("Harry and Tom's numbers were {} and {} respectively.".format(h, t))
```
3. Julian Gray 22 June 2016 at 9:30 am

Is there a different solution? Harry 5041, Tom 2116, 2116 being the only square with digit sum 10 among groups of three squares that share the same last two digits.

There are three triangular numbers with digit sum 10 that are in groups of three that share the same last two digits – 1711 with 2211-8911, 1540 with 3240-7140 and 1081 with 3081-7381. It seems that Tom’s 1081 is not unique in the way that is required.

Tom might have said that his last two digits were 11 or 40 or 81 but, since we don’t know what he said, we cannot see a way to choose between them. On the other hand we can find a solution if we assume that Tom had said that his last two digits were 16.

Or have I got my arithmetic and/or logic wrong?

• Jim Randell 22 June 2016 at 11:54 am

When were are looking groups of numbers that share the same last 2 digits we haven’t been told that the numbers share a digit sum. So we can’t use that information at that stage.

If we were told that Tom’s number ended in 16 then we can narrow it down to one of three squares (2116, 2916, 9216), but it could be one of four triangular numbers (2016, 3916, 6216, 9316). So Tom can’t have said his number ended in 16.

In fact Harry has to have a number ending in 41 (the only possibility that has two squares (5041, 6241) and two triangular numbers (3741, 6441)) and Tom has to have a number ending in 81 (the only possibility that has three squares (1681, 3481, 8281) and three triangular numbers (1081, 3081, 7381)).

The possible digits sums for Harry are 10 (5041), 13 (6241) and 15 (3741, 6441), and the possible digit sums for Tom are 10 (1081), 12 (3081), 16 (1681, 3481) and 19 (7381, 8281). So the common sum must be 10, and the actual numbers chosen follow.

• Julian Gray 22 June 2016 at 12:09 pm

Thanks so much for your trouble. Happy now.

This site uses Akismet to reduce spam. Learn how your comment data is processed.