# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1242: A long square

From New Scientist #2398, 7th June 2003 [link]

I wanted to square a three-figure number and I did it by long multiplication. The result was:

But in that array I have replaced all of the odd digits by dashes and all of the even digits by asterisks.

What 3-figure number was I squaring?

[enigma1242]

### 4 responses to “Enigma 1242: A long square”

1. Jim Randell 9 April 2015 at 8:18 am

This Python program runs in 33ms.

```from itertools import permutations
from enigma import nconcat, split, printf

even = (0, 2, 4, 6, 8)
odd  = (1, 3, 5, 7, 9)

# check the number matches the template sets
def check(n, *ss):
return all(n in s for (n, s) in zip(split(n, int), ss))

# the number we are squaring is a 3-digit number composed of even digits
# if it is abc, then abc x a = --**, abc x b = -***, abc x c = *--*
# so a, b and c are all distinct (and none of them is zero)
for (a, b, c) in permutations(even[1:], 3):
n = nconcat(a, b, c)
n2 = n * n
if n2 < 100000: continue

# check n^2 (the last digit must be even)
if not check(n2, even, odd, even, odd, odd): continue
# check the intermediate products
if not check(n * a, odd, odd, even, even): continue
if not check(n * b, odd, even, even, even): continue
if not check(n * c, even, odd, odd, even): continue

printf("n={n} [n2={n2}, {ps}]", ps=tuple(n * d for d in (a, b, c)))
```

Solution: The three figure number being squared is 486.

2. Naim Uygun 9 April 2015 at 12:23 pm
```even=['0','2','4','6','8']
odd=['1','3','5','7','9']
from itertools import permutations
for w in permutations("02468",3):
x="".join(w)

n=int(x)
p1=int(w[0])*n*100
s1=str(p1)
if  s1[0] in even or  s1[1] in even: continue

p2=int(w[1])*n*10
s2=str(p2)
if s2[0]  in even : continue

p3=int(w[2])*n
s3=str(p3)
if len(s3) != 4 : continue
if s3[1] in even  or s3[2] in even : continue

N=p1+p2+p3
print(" Three figure number=",n)
print(" The first product=",p1)
print(" The second product=",p2)
print(" The third product=",p3)
print(" Square=",N)

"""
Output:
Three figure number= 486
The first product= 194400
The second product= 38880
The third product= 2916
Square= 236196
"""

```
3. geoffrounce 9 April 2015 at 1:10 pm
```from itertools import permutations

for a, b, c in permutations ((2, 4, 6, 8, 0), 3):
if a == 0 or c == 0 :
continue
num = 100 * a + 10 * b + c
# check digit pattern for the last line
if tuple(int(x) % 2 for x in str(num ** 2)) == (0, 1, 0, 1, 1, 0):
# check digit pattern for first line
if tuple(int(x) % 2 for x in str(100 * a * num)) == (1, 1, 0, 0, 0, 0):
# check digit pattern for second line
if tuple(int(x) % 2 for x in str(10 * b * num)) == (1, 0, 0, 0, 0):
# check digit pattern for third line
if tuple(int(x) % 2 for x in str(c * num)) == (0, 1, 1, 0):
print('Three figure number =', num)

```
4. Paul 9 April 2015 at 4:09 pm

Here is my MMA code

```Timing[a=Select[Table[i,{i,100,999}],EvenQ[#]&&Mod[#,10]!=0&];
list=Reap[Do[l1=IntegerDigits[a[[q]] IntegerDigits[a[[q]]][[3]]];
l2=IntegerDigits[a[[q]] IntegerDigits[a[[q]]][[2]]];
l3=IntegerDigits[a[[q]] IntegerDigits[a[[q]]][[1]]];
Sow[{{a[[q]]},l1,l2,l3}],{q,1,Length[a]}]];
list=Flatten[list[[2]],1];
Cases[list,{{_?EvenQ},
{_?EvenQ,_?OddQ,_?OddQ,_?EvenQ},
{_?OddQ,_?EvenQ,_?EvenQ,_?EvenQ},
{_?OddQ,_?OddQ,_?EvenQ,_?EvenQ}}]]
{0.015600,{{{486},{2,9,1,6},{3,8,8,8},{1,9,4,4}}}}```

The difference with this program to the previous ones is we have no ‘If’ statements.
It is a table of 3 digit even numbers not ending in zero (line one), converted to digits and stored in a list with the format shown in the output. The last part of the program finds cases that match the pattern.