# Enigmatic Code

Programming Enigma Puzzles

## Enigma 147: Think of a number

From New Scientist #1292, 11th February 1982 [link]

“Think of a two-digit number.”

“Right.”

“Call it AB for the moment. Now think of a one-digit number.”

“Right.”

“Call it C. Does ABC make a two-digit number?”

“Yes, it does.”

“Call that one DE. Now write down the five-digit number ABCDE and divide it by 581. What do you get?”

“Well, AB was 45. C was 6 and so DE was 51. So my answer is 78.573149 or thereabouts.”

“Bad luck! If it had come out as a whole number, I would have paid for the drinks.”

What whole number would have done the trick?

This puzzle does not have a unique solution.

Enigma 52 and Enigma 262 are also called “Think of a number”.

[enigma147]

### 5 responses to “Enigma 147: Think of a number”

1. Jim Randell 23 November 2013 at 9:24 am

This Python program examines all 5-digit multiples of 581 to find the solution. It runs in 35ms.

from enigma import irange, printf

# consider 5-digit multiples of 581
for i in irange(18, 172):
n = i * 581
(ABC, DE) = divmod(n, 100)
(AB, C) = divmod(ABC, 10)
if AB + C == DE:
printf("{n} / 581 = {i} [AB={AB} C={C} DE={DE}]")

Solution: It is possible to end up with a whole number answer in two ways. You can arrive at 158 by choosing AB=91 and C=7, and this is the published solution. But by choosing AB=83 and C=0 (which is not disallowed by the problem statement) you can also get the answer of 143.

2. Naim Uygun 23 November 2013 at 12:34 pm

Here is a different approach:

from itertools import permutations
for a,b,c in permutations("9876543210",3):
if '0' in [a,c]: continue
ab=a+b
de=int(ab)+int(c)
if  9 < de < 100:
abcde=ab+c+str(de)
#if len(abcde) != 5: continue
n=int(abcde)
if n/581 !=  n//581:continue
print(n)

• Jim Randell 23 November 2013 at 4:14 pm

It does find the answer (and if you remove the assumption that C is non-zero it finds the second solution). But it also assumes that A, B and C are all different digits, which is the case in the answer, but it isn’t a requirement given in the puzzle statement.

(I’d also just use the % operator to check for divisibility in line 10:

if n % 581 > 0: continue

and that would make the program work in Python 2 as well).

3. geoffrounce 24 November 2013 at 11:43 am

This solution finds both answers of 91798 and 83083;

from itertools import permutations

for p in permutations ((9,8,7,6,5,4,3,2,1,0),3):
a,b,c = p
AB = b + a*10
C = c
if (AB + C) > 10 and (AB + C) < 100:
DE = AB + C
# find number ABCDE
A = AB//10; B = AB % 10
D = DE//10; E = DE % 10
ABCDE = E + D*10 + C*100 + B*1000 + A*10000
if ABCDE % 581 == 0 and ABCDE > 10000 :
print('Whole number = ', ABCDE)

• Jim Randell 24 November 2013 at 12:44 pm

I think the problem with using itertools.permutations() is that it only looks at cases where the digits in AB and C are all different, which isn’t required by the problem statement. (Although it does turn out to be the case in the answer(s)).

Instead you could use itertools.product(), which would check 1000 possibilities for (A, B, C), rather than the 720 checked by using itertools.permutations().

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