# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1291: Carrés Pairs et Impairs

From New Scientist #2449, 29th May 2004 [link]

In the following statements digits have been consistently replaced by capital letters, different letters being used for different digits:

UN and NEUF are odd perfect squares, ZERO is an even perfect square.

Please send in the numerical value of the square root of (UN × NEUF × ZERO).

Note: I am waiting for a phone line to be connected at my new house, so I only have sporadic access to the internet at the moment. The current estimate is that the line will be connected at the end of September 2014.

[enigma1291]

### 5 responses to “Enigma 1291: Carrés Pairs et Impairs”

1. Jim Randell 25 September 2014 at 3:07 pm

This Python program runs in 31ms

```from enigma import irange, is_duplicate, printf

# generate lists of squares with non-repeating digits as strings
def squares(lo, hi, step=1):
r = dict()
for i in irange(lo, hi, step=step):
s = str(i * i)
if not is_duplicate(s): r[s] = i
return r

# odd 2-digit squares
s1 = squares(5, 9, step=2)

# odd 4-digit squares
s2 = squares(33, 99, step=2)

# even 4-digit squares
s3 = squares(34, 98, step=2)

# choose UN
for (UN, r1) in s1.items():
# choose NEUF
for (NEUF, r2) in s2.items():
# check U and N match
if not(NEUF[2] == UN[0] and NEUF[0] == UN[1]): continue
# choose ZERO
for (ZERO, r3) in s3.items():
# check E matches
if not(ZERO[1] == NEUF[1]): continue
# and the rest are distinct
if len(set(UN + NEUF + ZERO)) != 7: continue
r = r1 * r2 * r3
printf("root={r} [UN={UN} NEUF={NEUF} ZERO={ZERO}]")
```

Solution: The square root of (UN × NEUF × ZERO) is 24948.

• Jim Randell 30 July 2016 at 11:33 pm

Puzzles like this can now be solved directly using the general Alphametic solver (SubstitutedExpression()) that is now part of the enigma.py library, without the need to write a program at all:

```% python -m enigma SubstitutedExpression \
--answer="is_square(UN * NEUF * ZERO)" \
"is_square(UN) % 2 = 1" \
"is_square(NEUF) % 2 = 1" \
"is_square(ZERO) % 2 = 0"
(is_square(UN) % 2 = 1) (is_square(NEUF) % 2 = 1) (is_square(ZERO) % 2 = 0)
(is_square(81) % 2 = 1) (is_square(1089) % 2 = 1) (is_square(7056) % 2 = 0) / E=0 F=9 N=1 O=6 R=5 U=8 Z=7 / 24948
is_square(UN * NEUF * ZERO) = 24948 [1 solution]
```

The whole thing runs in 69ms.

This shows the use of the --answer=<expr> parameter, which collects answers by the substituted value of the specified expression.

It also shows how values that cause the expressions we are searching on to throw exceptions are ignored. The is_square() function will return None if the argument is not a perfect square, which, in turn, will cause the mod() (%) function to throw a TypeError, as it has not been passed a number. However, the program that is generated by the SubstitutedExpression() solver ignores these errors and looks for other values that make the expression work without such errors.

One case where the error is not ignored is if a NameError is thrown. This is because you might accidentally mistype a function name, like "is_squared(UN)", and wonder why you don't get any solutions.

2. Naim Uygun 25 September 2014 at 4:00 pm
```#Answer: 24948
from itertools import permutations
for n,e,u,f,z,r,o in permutations(range(0,10),7):

if o%2 != 0: continue
if 0 in [n,u,z]:continue

neuf=n*1000+e*100+u*10+f
r1=neuf**0.5
if round(r1)**2 != neuf : continue

zero=z*1000+e*100+r*10+o
r2=zero**0.5
if round(r2)**2 !=zero: continue

un=10*u+n
r3=un**0.5
if round(r3)**2 != un : continue

```
3. Paul 27 September 2014 at 12:21 pm

a MMA version

```a1=IntegerDigits/@Table[n^2,{n,5,9,2}];
b1=IntegerDigits/@Table[n^2,{n,33,99,2}];
c1=IntegerDigits/@Table[n^2,{n,32,98,2}];
d=Tuples[{a1,b1,c1}];k=Cases[d,{{a_,b_},{c_,d_,e_,f_},{g_,h_,i_,j_}}/;
a==e&&b==c&&d==h&&Length[Union[{a,b,c,d,e,f,g,h,i,j}]]==7];
Print[Sqrt[Times@@FromDigits/@Flatten[k,1]]]//AbsoluteTiming
24948
{0.,Null}
```

Paul.

4. geoffrounce 31 July 2017 at 2:03 pm

Enigma 1104 provide a ready made Python programme template to solve this Enigma

```from itertools import permutations
from math import sqrt

def is_sq(n):
c = int(n**(1/2) + 0.5)
return (c**2 == n)

for p in permutations('1234567890',4):
N, E, U, F = p
if U == '0' or N == '0' : continue
UN = int(U + N)
NEUF = int(N + E + U + F)

if is_sq(UN) and UN % 2 == 1:
if is_sq(NEUF) and NEUF % 2 == 1:

r = set('1234567890').difference(p)
for q in permutations(r,3):
Z, R, O = q
if Z == '0': continue
ZERO = int(Z + E + R + O)
if is_sq(ZERO) and ZERO % 2 == 0:
print('UN = {}, NEUF = {}, ZERO = {}' \
.format(UN, NEUF,ZERO))

print('Square Root of (UN * NEUF * ZERO) = ', \
int(sqrt(UN * NEUF * ZERO)))

# UN = 81, NEUF = 1089, ZERO = 7056
# Square Root of (UN * NEUF * ZERO) =  24948
# Odd squares are 9^2 and 33^2 , even square is 84^2
```