# Enigmatic Code

Programming Enigma Puzzles

## Enigma 332: Shaken not stirred

From New Scientist #1480, 31st October 1985 [link]

Bond sipped his martini. “So you see”, continued M, “this new electronic door-pass means those famous zeros in front of your 007 codename are ignored.

“In fact, as you should know, with preliminary zeros ignored, all agent numbers become one-, two-, or three-digit numbers.”

“When an agent number is entered it must first be checked by the system to see that it is genuine. This is easily done as all agent numbers satisfy the following conditions. They are not only primes, but the first digit of each is a prime, as are the first two digits taken together in the three figure numbers.”

“For security reasons the building is divided into three functional areas, and not every agent number gains access to every area. For access to the equipment store, the digits of an agent’s number, when added successively to produce a single digit, must yield a prime.”

“For access to the administrative area, the digits must, when successively multiplied to produce a single digit, yield a prime.”

“For access to the operational area, the digits of the agent’s number must yield a prime whatever order they are read in.”

“Of course 1, not having a unique distinct factorisation, is not prime.”

“As you can see, the four double 0 agents, whose numbers are single digit primes, have access to all three areas. This leaves only one other number with access to the entire office; we have of course allocated this number to the one other employee who requires total access.”

The question is: what is the tea boy’s agent number?

[enigma332]

### One response to “Enigma 332: Shaken not stirred”

1. Jim Randell 12 February 2016 at 7:34 am

This Python program looks at the security credentials for each possible agent. It runs in 33ms.

```from itertools import permutations
from enigma import split, Primes, irange, multiply, join, printf

# compute digital roots
def digrt(n, fn=sum):
while n > 9:
n = fn(split(n, int))
return n

# 1-3 digit primes (as strings)
primes = list(str(n) for n in Primes(999))

# find valid agent numbers
agents = list()
for s in primes:
if any(s[:i] not in primes for i in irange(1, len(s) - 1)): continue
agents.append(s)

# determine access for each agent
aaa = list()
for s in agents:
n = int(s)
# the three access conditions
access = (
# the digital root obtained by adding the digits should be prime
str(digrt(n, fn=sum)) in primes,
# the digital root obtained by multiplying the digits should be prime
str(digrt(n, fn=multiply)) in primes,
# all anagrams should be prime
all(join(s) in primes for s in permutations(s))
)

if all(access): aaa.append(n)
printf("[agent {n:03d}: {access}]")

printf("[access all areas = {aaa}]")

teaboy = list(n for n in aaa if n > 9)
printf("teaboy = {teaboy}")
```

Solution: The tea boy is agent 311.