# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1510: Triangular threesome

From New Scientist #2672, 6th September 2008

Triangular numbers fit the formula n(n+1)/2, so the first five such numbers are 1, 3, 6, 10, 15. Harry, Tom and I have each chosen a different five-digit number that, like 3, is a triangular number and, rather like the word THREE, starts with four different digits and then repeats its fourth digit for its final digit.

The number that I have chosen has no digits in common with either of the numbers that Harry and Tom have chosen.

(a) Which number have I chosen?

(b) Which are the two numbers that Harry and Tom have chosen?

[enigma1510]

### 3 responses to “Enigma 1510: Triangular threesome”

1. Jim Randell 2 October 2012 at 7:14 am

The following Python program runs in 39ms.

```from itertools import count, combinations
from enigma import is_pairwise_distinct, printf

# make a set of triangular numbers that we're interested in
Ts = set()
for n in count(1):
t = n * (n + 1) / 2 # t is T[n] the nth triangular number
if t < 10000: continue # less than 5 digits, next
if t > 99999: break # more than 5 digits, done
t = str(t) # treat the number as a string
if t[3] != t[4]: continue # fourth and fifth digits must be the same
if not is_pairwise_distinct(*t[0:4]): continue # first 4 digits are different
printf("T[{n}] = {t}")

# pick D
for D in Ts:
d = set(D) # split D into digits
# pick H, T from the triangular numbers that don't share digits with D
for (H, T) in combinations((t for t in Ts if not d.intersection(t)), 2):
printf("D={D} H,T={H},{T}")
```

Solution: (a) 27966. (b) 15400 and 58311.

• Naim Uygun 2 October 2012 at 3:56 pm
```#By observing the numbers in the output:
#Answer: I have chosen the 27966
#Harry and Tom have chosen the numbers 15400,58311
from itertools import permutations
for p in permutations("1234567890",4):
t=int(p[0])
if t==0:continue
h=int(p[1])
r=int(p[2])
e=int(p[3])
three=10000*t+1000*h+100*r+10*e+e
root=(-1+(1+8*three)**0.5)/2
if root  != int(root) : continue
print(three)
```
2. geoffrounce 14 September 2017 at 3:24 pm
```% A Solution in MiniZinc
include "globals.mzn";

var 1..9:a;   var 0..9:b;   var 0..9:c;    var 0..9:d;  % My number
var 1..9:e;   var 0..9:f;   var 0..9:g;    var 0..9:h;  % Harry's number
var 1..9:i;   var 0..9:j;   var 0..9:k;    var 0..9:l;  % Tom's number

var 10000..99999 : abcdd = 10000 * a + 1000 * b +  100 * c + 10 * d + d;
var 10000..99999 : efghh = 10000 * e + 1000 * f +  100 * g + 10 * h + h;
var 10000..99999 : ijkll = 10000 * i + 1000 * j +  100 * k + 10 * l + l;

% My number has no digits in common with either of the other two numbers
constraint all_different ( [a, b, c, d, e, f, g, h] )
/\ all_different ( [a, b, c, d, i, j, k, l] ) /\ efghh != ijkll;

% Set of 5-digit triangular numbers
set of int: tri5 = { n * (n+1) div 2 | n in 141..446 };

constraint abcdd in tri5 /\ efghh in tri5 /\ ijkll in tri5;

solve satisfy;

output [ "My number is " ++ show(abcdd) ]
++  [ "\nHarry and Tom's numbers are " ++ show(efghh) ++ " and " ++ show(ijkll) ];

% My number is 27966
% Harry and Tom's numbers are 58311 and 15400
% ----------
% Finished in 69msec
```

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