# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1577: Happy New Year

From New Scientist #2742, 9th January 2010 [link]

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

What, in ascending order, are the three numbers?

[enigma1577]

### 4 responses to “Enigma 1577: Happy New Year”

1. Jim Randell 9 February 2012 at 11:14 am

The following Python program runs in 38ms.

```from enigma import irange, printf

# 3-digit square numbers
for i in irange(11, 31):
n = i * i
d1 = set(str(n))
if not len(d1) == 3: continue

# 3-digit triangular numbers
for j in irange(14, 44):
t = j * (j+1) // 2
d2 = d1.union(set(str(t)))
if not len(d2) == 6: continue

# remaining number
r = 2010 - (n + t)
if not(99 < r < 1000): continue
d3 = d2.union(set(str(r)))
if not len(d3) == 9: continue
printf("{i}^2 = {n}, T[{j}] = {t}, r = {r}")
```

Solution: The numbers are: 325, 784 and 901.

2. geoffrounce 5 August 2017 at 7:29 pm
```% A Solution in MiniZinc
include "globals.mzn";

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

constraint all_different ( [A, B, C, D, E, F, G, H, I]);

% Form the three numbers in ascending order
var 123..987: ABC = 100*A + 10*B + C;
var 123..987: DEF = 100*D + 10*E + F;
var 123..987: GHI = 100*G + 10*H + I;

constraint ABC < DEF /\ DEF < GHI;

% Form sets of 3-digit square and 3-digit triangular numbers
set of int: sq3 = { pow(x, 2) | x in 10..31 };
set of int: tri3 = { n * (n+1) div 2 | n in 14..44 };

% One of the numbers is a perfect square and another is a triangular number
constraint sum( [ABC in sq3, DEF in sq3, GHI in sq3]) == 1 /\
sum( [ABC in tri3, DEF in tri3, GHI in tri3] ) == 1;

% The sum of the three numbers is 2010.
constraint ABC + DEF + GHI == 2010;

solve satisfy;

output ["Three numbers are " ++  show(ABC) ++ ", " ++ show(DEF) ++ " and " ++ show(GHI) ];

% Three numbers are 325, 784 and 901
% Finished in 60msec
```
3. hakank 7 August 2017 at 6:12 am

Here is a Picat port of @geoffrounce MiniZinc model. It runs in about 18ms.

```import cp.
main => go.
go ?=>
L = [A,B,C,D,E,F,G,H,I],  L :: 0..9,
all_different(L),

ABC :: 123..987, ABC #= 100*A + 10*B + C,
DEF :: 123..987, DEF #= 100*D + 10*E + F,
GHI :: 123..987, GHI #= 100*G + 10*H + I,
All = [ABC,DEF,GHI],
sum(All) #= 2010,
increasing(All),

Sq3  = [ X**2 : X in 10..31],
Tri3 = [ N * (N+1) div 2 : N in 14..44],
sum([X :: Sq3 #<=> 1  : X  in All]) #= 1,
sum([X :: Tri3 #<=> 1 : X  in All]) #= 1,

solve(\$[ff,split],L),
println([ABC,DEF,GHI]),
fail,
nl.

go => true.
```

Picat don’t support MiniZinc’s nice syntax of “in” in

```sum( [ABC in sq3, DEF in sq3, GHI in sq3]) == 1
```

However, it can be simulated by the uglier ” X :: Sq3 # 1″. The “fail” predicate ensures that there’s a unique solution.

Below are two different approaches. go2/0 is also a plain CP model and is actually some milliseconds faster than go/0, but not as neat. go3/0 is a hybrid where the perfect square and triangle number conditions are calculated after solve/2. It’s about as fast as go/0.

```go2 =>

L = [A,B,C,D,E,F,G,H,I],
L :: 0..9,
all_different(L),

ABC :: 123..987, ABC #= 100*A + 10*B + C,
DEF :: 123..987, DEF #= 100*D + 10*E + F,
GHI :: 123..987, GHI #= 100*G + 10*H + I,

ABC + DEF + GHI #= 2010,

All = [ABC,DEF,GHI],
increasing(All),

Sq3  = [ X**2 : X in 10..31],
Tri3 = [ N * (N+1) div 2 : N in 14..44],
TriIx :: 0..3, SqIx :: 0..3,
(ABC :: Sq3 , SqIx #= 1 ; DEF :: Sq3 , SqIx #= 2 ; GHI :: Sq3 , SqIx #= 3),
(ABC :: Tri3 , TriIx #= 1 ; DEF :: Tri3 , TriIx #= 2 ; GHI :: Tri3 , TriIx #= 3),
TriIx #> 0, SqIx #> 0,
TriIx #!= SqIx,
solve(\$[ff,split],L),
println([ABC,DEF,GHI]),
fail,
nl.

go3 =>
L = [A,B,C,D,E,F,G,H,I],
L :: 0..9,
all_different(L),

ABC :: 123..987, ABC #= 100*A + 10*B + C,
DEF :: 123..987, DEF #= 100*D + 10*E + F,
GHI :: 123..987, GHI #= 100*G + 10*H + I,

All = [ABC,DEF,GHI],
increasing(All),
ABC + DEF + GHI #= 2010,

solve(\$[ff,split],L),

CC = 0,
Sq3  = [ X**2 : X in 10..31],
Tri3 = [ N * (N+1) div 2 : N in 14..44],
if ABC :: Sq3 then CC := CC + 1 end,
if DEF :: Sq3 then CC := CC + 1 end,
if GHI :: Sq3 then CC := CC + 1 end,
if ABC :: Tri3 then CC := CC + 1 end,
if DEF :: Tri3  then CC := CC + 1 end,
if GHI :: Tri3 then CC := CC + 1end,
CC == 2,
println([ABC,DEF,GHI]),
fail,
nl.
```
4. Jim Randell 7 August 2017 at 9:12 am

Here’s a solution using the SubstitutedExpression() alphametic solver from the enigma.py library. This run files executes in 94ms.

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

SubstitutedExpression

# neaten up the output
--template="(is_square(ABC)) (is_triangular(DEF)) (ABC + DEF + GHI = 2010)"
--solution=""

# the answer is the numbers in ascending order

# ABC is the square
"is_square(ABC)"

# DEF is the triangular number
"is_triangular(DEF)"

# GHI is the other number
"2010 - ABC - DEF = GHI"
```

The --template and --solution parameters neaten up the output and are not necessary to solve the problem. The --answer parameter ensures the solution numbers are given in ascending order.

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