# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1651: Four-digit numbers

From New Scientist #2817, 18th June 2011 [link]

I have found a 3-digit perfect square, a 3-digit perfect cube and a 4-digit triangular number that between them use 10 different digits, with no leading zero. (Triangular numbers are those that fit the formula n(n+1)/2, like 1, 3, 6, 10, 15.)

I have also found a 3-digit perfect square, a 3-digit triangular number and a 4-digit perfect cube that between them use 10 different digits, with no leading zero.

What are my two 4-digit numbers?

[enigma1651]

### 2 responses to “Enigma 1651: Four-digit numbers”

1. Jim Randell 5 December 2011 at 4:58 pm

The following Python code runs in 32ms.

```from itertools import count

def create(n, f):
"create(n, f): create n-(distinct-)digit numbers satisfying f"
r = []
for i in count():
x = f(i)
if len(str(x)) < n: continue
if len(str(x)) > n: break
if len(set(list(str(x)))) < n: continue
r.append(x)
return r

def combination(a, b, c):
"combination(a, b, c): choose an element from each list"
for x in a:
s1 = set(list(str(x)))
for y in b:
s2 = set(list(str(y)))
if not s2.isdisjoint(s1): continue
for z in c:
s3 = set(list(str(z)))
if not(s3.isdisjoint(s1) and s3.isdisjoint(s2)): continue
yield (x, y, z)

# 3-digit squares
s = lambda x: pow(x, 2)
squares = create(3, s)

# find 3-digit cubes
c = lambda x: pow(x, 3)
cubes = create(3, c)

# find 4-digit triangles
t = lambda x: (x * (x + 1)) // 2
triangles = create(4, t)

# find a combination
for s in combination(squares, cubes, triangles):
print(1, s)

# and now the second combination
triangles = create(3, t)
cubes = create(4, c)
for s in combination(squares, triangles, cubes):
print(2, s)
```

Solution: The four digit numbers are 6903 and 4913.

2. geoffrounce 14 August 2017 at 8:37 am
```% A Solution in MiniZinc
include "globals.mzn";

% 1st set of digits
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;   var 0..9:J;

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

% 2nd set of digits
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;   var 0..9:j;

constraint all_different( [a, b, c, d, e, f, g, h, i, j] );

% 1st set of numbers(sq3, cb3, tri4)
var 100..999 : ABC = 100*A + 10*B + C;
var 100..999 : DEF = 100*D + 10*E + F;
var 1000..9999 : GHIJ = 1000*G + 100*H + 10*I + J;

% 2nd set of numbers (sq3, tri3, cb4)
var 100..999 : abc = 100*a + 10*b + c;
var 100..999 : def = 100*d + 10*e + f;
var 1000..9999 : ghij = 1000*g + 100*h + 10*i + j;

set of int: sq3 = {n*n | n in 10..31};

set of int: cb3 = {n*n*n | n in 5..9} ;
set of int: cb4 = {n*n*n | n in 10..21} ;

set of int: tri3 = { n * (n+1) div 2 | n in 14..44 };
set of int: tri4 = { n * (n+1) div 2 | n in 45..140 };

% 1st digit set
constraint ABC in sq3 /\ DEF in cb3 /\ GHIJ in tri4;

% 2nd digit set
constraint abc in sq3 /\ def in tri3 /\ghij in cb4;

solve satisfy;

output ["Two 4 digit numbers are " ++ show(GHIJ) ++ " and " ++ show(ghij) ++ "\n" ]
++ ["ABC(sq3) = " ++ show(ABC) ++ ",  DEF(cb3) = " ++ show(DEF) ++ ",  GHIJ(tri4) = " ++ show(GHIJ) ++ "\n" ]
++ ["abc(sq3) = " ++ show(abc) ++ ",  def(tri3) = " ++ show(def) ++ ",  ghij(cb4) = " ++ show(ghij)];

% Two 4 digit numbers are 6903 and 4913
% ABC(sq3) = 784,  DEF(cb3) = 125,  GHIJ(tri4) = 6903
% abc(sq3) = 256,  def(tri3) = 780,  ghij(cb4) = 4913
% Finished in 72msec

```