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]

4 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 or 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
```
• geoffrounce 7 September 2017 at 1:17 pm

Here is a solution in MiniZinc:

```% A Solution in MiniZinc
include "globals.mzn";

var 0..9:A; var 0..9:B; var 0..9:C; var 0..9:D; var 0..9:E;
var 0..9:F; var 0..9:G; var 0..9:H; var 0..9:I;

constraint alldifferent([A,B,C,D,E,F,G,H,I]) /\ A > 0 /\ D > 0 /\ G > 0;

var 100..999: ABC = 100*A + 10*B + C;
var 100..999: DEF = 100*D + 10*E + F;
var 100..999: GHI = 100*G + 10*H + I;

set of int: sq3 = {x*x | x in 10..31};
set of int: tri3 = {n*(n+1) div 2 | n in 14..44};

constraint ABC in sq3 /\ DEF in tri3 /\ ABC + DEF+ GHI == 2011;

solve satisfy;

output[ "Three Numbers (square, triangular, other) are: "++ show(ABC) ++ ", "
++ show(DEF) ++ ", " ++ show(GHI)];

% Three Numbers (square, triangular, other) are: 841, 903, 267
% ie in ascending order are 267, 841, 903
% Finished in 69msec

```
3. Jim Randell 7 September 2017 at 8:03 am

Here is a solution using the SubstitutedExpression() solver from the enigma.py library.

This run-flle executes in 95ms.

```#!/usr/bin/env python -m enigma -r

SubstitutedExpression