# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1476: Portuguese squares

From New Scientist #2638, 12th January 2008

Zero, um, nove and cem are the Portuguese for 0, 1, 9 and 100; so it is appropriate that I can make the following statement: ZERO, UM, NOVE and CEM are perfect squares.

In this statement digits have been consistently replaced by capital letters, different letters being used for different digits. No number starts with a zero.

Find the numerical value of the square root of (ZERO × UM × NOVE × CEM).

[enigma1476]

### 3 responses to “Enigma 1476: Portuguese squares”

1. Jim Randell 4 December 2012 at 3:56 pm

Originally I searched the squares and matched up the letters, which may be a bit faster (it ran in 35ms), but my normal way of using `itertools.permutations()` results in a more compact program, and it runs in 36ms.

```from itertools import permutations
from enigma import irange, is_duplicate, multiply, is_square, printf

# we need 2, 3 and 4 digit squares (with no repeated digits)
s = [[] for i in range(5)]
for i in irange(4, 99):
n = str(i * i)
if is_duplicate(n): continue
s[len(n)].append(n)

ds = set('0123456789')
for ZERO in s:
(Z, E, R, O) = ZERO

ds1 = ds.difference((Z, E, R, O))
for (N, V) in permutations(ds1, 2):
NOVE = N + O + V + E
if NOVE not in s: continue

ds2 = ds1.difference((N, V))
for (C, M) in permutations(ds2, 2):
CEM = C + E + M
if CEM not in s: continue

ds3 = ds2.difference((C, M))
for U in ds3:
UM = U + M
if UM not in s: continue

# compute the result
r = is_square(multiply(int(s) for s in (UM, CEM, NOVE, ZERO)))
printf("{r} [UM={UM} CEM={CEM} NOVE={NOVE} ZERO={ZERO}]")
```

Solution: The value of the square root is 1240092.

• Jim Randell 5 July 2016 at 9:33 am

Using the general alphametic solver from the enigma.py library means you don’t have to write a program at all:

```% python -m enigma SubstitutedExpression \
"is_square(ZERO)" \
"is_square(UM)" \
"is_square(NOVE)" \
"is_square(CEM)"
(is_square(ZERO)) (is_square(UM)) (is_square(NOVE)) (is_square(CEM))
(is_square(9604)) (is_square(81)) (is_square(5476)) (is_square(361)) / C=3 E=6 M=1 N=5 O=4 R=0 U=8 V=7 Z=9
```

It runs in 76ms, and gives us the assignment of letters to digits, then a simple calculation gives the solution.

2. geoffrounce 19 December 2017 at 12:28 pm
```% A Solution in MiniZinc
include "globals.mzn";

var 0..9:Z;  var 0..9:E;  var 0..9:R;  var 0..9:O;  var 0..9:U;
var 0..9:M;  var 0..9:N;  var 0..9:V;  var 0..9:C;

constraint Z != 0 /\ U != 0 /\ N != 0 /\ C != 0
/\ all_different( [Z, E, R, O, U, M, N, V, C] );

predicate is_square(var int: y) =
exists(z in 1..ceil(sqrt(int2float(ub(y))))) ( z*z = y );

constraint is_square(Z*1000 + 100*E + 10*R + O)  /\ is_square(10*U + M)
/\ is_square(N*1000 + 100*O + 10*V + E) /\ is_square(100*C + 10*E + M);

solve satisfy;

output ["Square root of (ZERO × UM × NOVE × CEM) is " ++ show( round( fix( sqrt(
(1000*Z + 100*E + 10*R + O) * (10*U + M) * (1000*N + 100*O + 10*V + E)
* ( 100*C + 10*E + M) ) ) ) ) ];

% Square root of (ZERO × UM × NOVE × CEM) is 1240092
% ----------
% Finished in 124msec
```

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