# Enigmatic Code

Programming Enigma Puzzles

## Enigma 291: An Enigma from the past

From New Scientist #1439, 17th January 1985 [link]

My old schoolteacher used to set us Enigma-type puzzles. For example, he once said the following:

“I have in mind a prime number between 4 and 50. I shall answer some questions about it and leave you to work out what the number is.”

We asked, “Is it one more than a multiple of 3?”

“Is it one, two or three more than a perfect square?” and “Is the sum of its digits odd?”

These he answered yes or no as appropriate and from his three answers we were able to work out the number.

In fact, now that I come to think about it, with all the above information the answer to the second question alone would be enough to enable you to work out the number.

What is it?

[enigma291]

### 2 responses to “Enigma 291: An Enigma from the past”

1. Jim Randell 30 June 2015 at 7:34 am

This program uses the [[ filter_unique() ]] function from the enigma.py library to consider the statements for each prime. It runs in 33ms.

from enigma import irange, Primes, split, filter_unique, printf

# squares
squares = set(i * i for i in irange(1, 7))

# candidate primes (between 4 and 50)
primes = Primes(50).generate(start=4)

# the statements

# 1. is it one more than a multiple of three?
s1 = lambda p: (p % 3 == 1)

# 2. is it one, two or three more than a perfect square?
s2 = lambda p: any(n in squares for n in (p - 1, p - 2, p - 3))

# 3. is the sum of its digits odd?
s3 = lambda p: (sum(split(p, int)) % 2 == 1)

# the outcome of the statements determines the value
(primes, _) = filter_unique(primes, lambda p: (s1(p), s2(p), s3(p)))

# from the remaining primes s2 can be used to determine the answer
(primes, _) = filter_unique(primes, s2)

printf("solutions = {primes}")

Solution: The number is 43.

• geoffrounce 17 February 2016 at 5:54 pm
Jim - thanks for helping with completion of the logic
for a manual solution

Prime candidates are [5,7,11,13,17,19,23,29,31,37,41,43,47]

I thought the answer to this enigma might be apparent if I
formed a matrix of prime numbers against the three
questions given - labelled Q1, Q2 and Q3 below;

Q1: Is it 1 more than a multiple of 3 ?

Q2: Is it 1, 2, or 3 more than a multiple of 3 ?

Q3: Is the sum of its digits odd ?

Prime   Q1      Q2   Q1&Q2     Q3     Q1&Q2&Q3
5       N       Y     NY       Y        NYY
7       Y       Y     YY       Y        YYY
11      N       Y     NY       N        NYN
13      Y       N     YN       Y        YNY
17      N       Y     NY       N        NYN
19      Y       Y     YY       N        YYN
23      N       N     NN       Y        NNY
29      N       N     NN       Y        NNY
31      Y       N     YN       N        YNN
37      Y       Y     YY       N        YYN
41      N       N     NN       Y        NNY
43      Y       N     YN       Y        YNY
47      N       N     NN       Y        NNY

The results after three questions (Q1&Q2&Q3) I get are:

NYY - 5
YYY - 7
NYN - 11, 17
YYN - 19, 37
NNY - 23, 29, 41,4 7
YNY - 43
YNN - 13, 31

From the three single answers we can work out the number. We don't know what
the answers were, but we can work out that the number must be one of:

5 =  NYY
7 =  YYY
43 = YNY

We are then told that if we knew the answer to just the second question
we could work out the number.

So the number must be 43, as that is the only remaining number that corresponds
to Q2 = N. For Q2 = Y the number could be either 5 or 7.

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