# Enigmatic Code

Programming Enigma Puzzles

## Enigma 439: Ten to twenty

From New Scientist #1589, 3rd December 1987 [link]

“How many perfect squares are there between TEN and TWENTY?”

“ONE.”

“Right. And are TWO, TEN, TWELVE and TWENTY even?”

“Of course. In fact the first and last digits of TWENTY are both even.”

“Right. And is TEN divisible by 3?”

“Of course not.”

In the above, digits have consistently been replaced by letters, different letters representing different digits.

Find NOW.

[enigma439]

### 2 responses to “Enigma 439: Ten to twenty”

1. Jim Randell 9 March 2018 at 6:35 am

This Python program defines a function to calculate the number of perfect squares between two numbers, and then uses the [[ `SubstitutedExpression()` ]] solver from the enigma.py library to solve the alphametic part of the problem. It runs in 81ms.

Run: [ @repl.it ]

```from enigma import isqrt, SubstitutedExpression

# return the number of squares between a and b (inclusive, a < b)
def nsquares(a, b):
if b < a: return 0
# the number of squares between 0 and n (inclusive) is isqrt(n) + 1
i = isqrt(b)
return (i + 1 if a == 0 else i - isqrt(a - 1))

# invalid digits
d2i = { 0: 'NOT' } # no leading zeros
for d in (1, 3, 5, 7, 9): d2i[d] = 'ENOTY' # even numbers

# solve the alphametic expressions
SubstitutedExpression(
[ "nsquares(TEN, TWENTY) = ONE", "TEN % 3 != 0" ],
env=dict(nsquares=nsquares),
d2i=d2i,
).go()
```

Solution: NOW = 869.

The assignment of letters to digits is:

E=2 N=8 O=6 T=4 W=9 Y=0

There are two further solutions where TEN is a multiple of 3:

E=0 N=2 O=6 T=4 W=1 Y=8
E=0 N=8 O=6 T=4 W=9 Y=2

2. Brian Gladman 9 March 2018 at 8:43 am
```from itertools import permutations

# permute the even digits for letters known to be even
for E, N, O, T, Y in permutations(range(0, 10, 2), 5):
# check that T is non-zero and that 100 is not
# divisible by three
ten = 100 * T + 10 * E + N
if T and ten % 3:
one = 100 * O + 10 * N + E
t_enty = 100010 * T + 1000 * E + 100 * N + Y
# consider even digits for W
for W in range(1, 10, 2):
# find how many squares are between TEN and TWENTY
twenty = 10000 * W + t_enty
if int(twenty ** 0.5) - int(ten ** 0.5) == one:
print(f'NOW = {100 * N + 10 * O + W}')
```

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