Enigmatic Code

Programming Enigma Puzzles

Enigma 1458: Imagine…

From New Scientist #2619, 1st September 2007

Look how the word ‘Imagine’ can be transformed into ‘Enigma’:

I M A G I N E

Delete the first:

M A G I N E

Reverse the next two:

A M G I N E

Reverse completely:

E N I G M A

Your task is to start with a particular seven-figure number, carry out those three operations on its digits and end up with a six-figure number that is precisely one-third of the number that you started with.

What is that seven-figure number?

[enigma1458]

4 responses to “Enigma 1458: Imagine…”

1. Jim Randell 26 February 2013 at 8:18 am

This brute force approach in Python runs in 141ms (under PyPy).

```# if we start with ABCDEFG and peform the operations we end up with
# GFEDBC, and this is one third of the original number.

# so: GFEDBC x 3 = ABCDEFG

from enigma import irange, printf

for g in irange(1 + 1000000 // 3, 999999):
a = g * 3
# check the corresponding digits match
(A, G) = (str(a), str(g))
if A[1:3] == G[4:6] and A[3:] == G[3::-1]:
printf("ABCDEFG = {a}, GFEDBC = {g}")
```

Solution: The seven-figure number is 2939979.

• Jim Randell 26 February 2013 at 8:48 am

Considered as an addition sum, we get a slightly longer, but much faster program. This version runs in 37ms.

```# if we start with ABCDEFG and perform the operations we end up with
# GFEDBC, and this is one third of the original number.

# so: GFEDBC x 3 = ABCDEFG

# considered as an addition sum:
#  GFEDBC
#  GFEDBC
#  GFEDBC
# -------
# ABCDEFG

from enigma import irange, printf

for C in irange(1, 9):
(c1, G) = divmod(3 * C, 10)
for B in irange(0, 9):
(c2, F) = divmod(3 * B + c1, 10)
for D in irange(0, 9):
(c3, E) = divmod(3 * D + c2, 10)
(c4, d) = divmod(3 * E + c3, 10)
if d != D: continue
(c5, c) = divmod(3 * F + c4, 10)
if c != C: continue
(A, b) = divmod(3 * G + c5, 10)
if A == 0 or b != B: continue

printf("{A}{B}{C}{D}{E}{F}{G} = {G}{F}{E}{D}{B}{C} x 3")
```
• Naim Uygun 26 February 2013 at 1:46 pm

Can the seven-digit number be 3996999 ?

• Jim Randell 26 February 2013 at 2:11 pm

No. 3996999 ÷ 3 = 1332333, which is a 7 figure number (not a 6 figure number). The result of operations on 3996999 is 999699 (a 6 figure number), and the two are clearly not equal.