# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1674: Four months

From New Scientist #2840, 26th November 2011 [link]

In a normal year, February has 28 days, April has 30 and January and May both have 31. Using letters for digits consistently, JAN and MAY are divisible by 31, APR is divisible by 30, and FEB is divisible by 28.

What is the three-digit number FEB?

[enigma1674]

### 3 responses to “Enigma 1674: Four months”

1. Jim Randell 30 November 2011 at 9:42 am

It is obvious that if APR is divisible by 30 then R=0, and also if FEB is divisible by 28 then B is in {2, 4, 6, 8}.

The remaining space can be checked with a simple Python program in 120ms:

```from itertools import permutations
from enigma import printf

R = 0
for B in (2, 4, 6, 8):
for (J, A, N, M, Y, P, F, E) in permutations(set(range(1, 10)).difference((B,)), 8):

JAN = J*100 + A*10 + N
if JAN % 31: continue

MAY = M*100 + A*10 + Y
if MAY % 31: continue

APR = A*100 + P*10 + R
if APR % 30: continue

FEB = F*100 + E*10 + B
if FEB % 28: continue

printf("JAN={JAN} MAY={MAY} APR={APR} FEB={FEB}")
```

Solution: FEB = 728.

• Jim Randell 30 November 2011 at 10:42 am

Using a longer program to do more early rejection I can get the execution time down to 35ms.

```from itertools import permutations
from enigma import printf

R = 0
s1 = set(range(1, 10))
for B in (2, 4, 6, 8):
s2 = s1.difference((B,))
for (F, E) in permutations(s2, 2):
FEB = F*100 + E*10 + B
if FEB % 28: continue

s3 = s2.difference((F, E))
for (A, P) in permutations(s3, 2):
APR = A*100 + P*10 + R
if APR % 30: continue

s4 = s3.difference((A, P))
for (J, N) in permutations(s4, 2):
JAN = J*100 + A*10 + N
if JAN % 31: continue

s5 = s4.difference((J, N))
for (M, Y) in permutations(s5, 2):
MAY = M*100 + A*10 + Y
if MAY % 31: continue

printf("JAN={JAN} MAY={MAY} APR={APR} FEB={FEB}")
```
2. Jim Randell 28 June 2016 at 8:59 am

Using the generalised Alphametic solver in enigma.py we can solve this problem directly without needing to write a program:

```% python -m enigma SubstitutedExpression \
"JAN % 31 = 0" \
"MAY % 31 = 0" \
"APR % 30 = 0" \
"FEB % 28 = 0"
(JAN % 31 = 0) (MAY % 31 = 0) (APR % 30 = 0) (FEB % 28 = 0)
(465 % 31 = 0) (961 % 31 = 0) (630 % 30 = 0) (728 % 28 = 0) / A=6 B=8 E=2 F=7 J=4 M=9 N=5 P=3 R=0 Y=1
(961 % 31 = 0) (465 % 31 = 0) (630 % 30 = 0) (728 % 28 = 0) / A=6 B=8 E=2 F=7 J=9 M=4 N=1 P=3 R=0 Y=5
```

It executes in 69ms.

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