# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1629: Another new year

From New Scientist #2795, 15th January 2011 [link]

I have written down three three-digit integers which between them use nine different digits. One of the integers is a perfect square and another is a triangular number. The sum of the three integers is 2011.

What, in ascending order, are the three integers?

[enigma1629]

### 2 responses to “Enigma 1629: Another new year”

1. jimrandell 13 December 2011 at 11:14 pm

The following Python program runs in 30ms.

```from itertools import count
from enigma import irange, printf

# three digit squares
squares = list()
for i in irange(10, 31):
n = i * i
# but not with repeated digits
if len(set(str(n))) < 3: continue
squares.append(n)

# and three digit triangular numbers
triangles = list()
for i in count(0):
n = i * (i + 1) // 2
s = str(n)
if len(s) < 3: continue
if len(s) > 3: break
# and not with repeated digits
if len(set(s)) < 3: continue
triangles.append(n)

for a in squares:
for b in triangles:
c = 2011 - (a + b)
if c < 100: continue
if c > 999: continue
s = set(str(a)).union(str(b)).union(str(c))
if len(s) < 9: continue
printf("{a} + {b} + {c} = 2011")
```

Solution: The three integers are 267, 841 and 903.

2. geoffrounce 10 September 2016 at 9:54 am
```# form lists of 3-digit squares and 3-digit triangular numbers
sq3 = [ x * x for x in range(10,33)]
tri3 = [ n * (n + 1)//2 for n in range(14,46)]

digits = set('1234567890')

from itertools import permutations

for p1 in permutations('1234567890',3):
a, b, c = p1
if a == '0': continue
# form  1st number
abc = int(a + b + c)
if abc not in sq3: continue

p2 = digits.difference(p1)
for q in permutations(p2, 3):
# form 2nd number
D, E, F  = q
if D == '0' : continue
# we can't use def as a variable name
DEF = int(D + E + F)
if DEF not in tri3: continue

p3 = p2.difference(q)
for r in permutations(p3,3):
g,h,i = r
if g == '0': continue
# form 3rd number
ghi = int(g + h + i)
if abc + DEF + ghi == 2011:
nums = (abc, DEF, ghi)
nums = sorted(nums)
print ('The three numbers are: {}, {} and {}' \
.format(nums[0], nums[1], nums[2]))

# The three numbers are: 267, 841 and 903
```