# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1640: Four-cast

From New Scientist #2806, 2nd April 2011 [link]

Fill each of the 16 boxes with a digit so that the same four consecutive digits appear in each row and in each column. In this way you can then read four 4-figure numbers across the rows and four 4-figure numbers down the columns. You must choose your digits so all eight 4-figure numbers are different and none of them has any odd factor between 2 and 20.

What are the lowest and the highest of your eight 4-figure numbers?

[enigma1640]

Advertisements

### 2 responses to “Enigma 1640: Four-cast”

1. jimrandell 11 December 2011 at 1:02 pm

This program generates all possible squares and checks for ones that fit the conditions (runtime: 136ms), although if you note the simplification in the code you can get this down to 78ms.

```from itertools import permutations
from enigma import irange, is_distinct, factor, concat, printf

factors = set(irange(3, 20, step=2))
solution = {}

def check(s, l):
ns = []
for i in l:
n = int(''.join(map(lambda x: s[(ord(x)-ord('A'))], list(i))))
if factors.intersection(factor(n)): return False
ns.append(n)
print(ns)
solution[' '.join(map(str, sorted(ns)))] = 1
return True

S = set('ABCD')
P = list(permutations(S, 4))

for r1 in P:
for r2 in P:
if any(r1[i] == r2[i] for i in range(4)): continue
for r3 in P:
if any(r3[i] in (r1[i], r2[i]) for i in range(4)): continue

r4 = (
S.difference((r1[0], r2[0], r3[0])).pop(),
S.difference((r1[1], r2[1], r3[1])).pop(),
S.difference((r1[2], r2[2], r3[2])).pop(),
S.difference((r1[3], r2[3], r3[3])).pop(),
)

C1 = concat(r1[0], r2[0], r3[0], r4[0])
C2 = concat(r1[1], r2[1], r3[1], r4[1])
C3 = concat(r1[2], r2[2], r3[2], r4[2])
C4 = concat(r1[3], r2[3], r3[3], r4[3])

R1 = concat(*r1)
R2 = concat(*r2)
R3 = concat(*r3)
R4 = concat(*r4)

if not is_distinct(C1, R1, R2, R3, R4): continue
if not is_distinct(C2, R1, R2, R3, R4): continue
if not is_distinct(C3, R1, R2, R3, R4): continue
if not is_distinct(C4, R1, R2, R3, R4): continue

# NOTE: we could eliminate the sequences with '5' in
# as any number ending in ...5 would be divisible by
# 5 and hence excluded.
# Also 6+7+8+9 = 30, so all numbers from that sequence
# would be divisible by 3.
# this would leave '1234' as the only possible candidate.
cs = ('1234', '2345', '3456', '4567', '5678', '6789')
l = (R1, R2, R3, R4, C1, C2, C3, C4)
for c in cs:
check(c, l)

for s in solution.keys():
ns = map(int, s.split())
printf("min={ns[0]} max={ns[-1]} [{s}]")
```

Solution: The lowest number is 1324. The highest number is 4231.

• Hugh Casement 28 April 2016 at 8:47 am

It seems the rows are 1324, 4231, 2143, 3412 in four possible orders, the columns 1423, 3214, 2341, 4132 in a corresponding order — or of course one can swap rows and columns.  Twelve of the twenty-four possible permutations of the four digits have odd factors less than 21.  1234, 1432, 3142, and 4321 would be allowed but don’t occur in practice, presumably because they would inevitably cross with a forbidden number.