Place nine different digits (from 0-9) into a 3x3 grid, such that the eight sums formed by the three rows, three columns and two diagonals are consecutive numbers.
Two different solutions are:
194 14
873 18
605 11
15 16 12
13 17
318 12
920 11
465 15
16 9 13
10 14
where the row totals appear to the right of each row, and then the three column totals appear, and on the final line, the two diagonal totals.
The complete computer output included all rotations and reflections of each of these, for 16 overall:
186 15
970 16
435 12
14 18 11
13 17
194 14
873 18
605 11
15 16 12
13 17
318 12
920 11
465 15
16 9 13
10 14
394 16
126 9
805 13
12 11 15
10 14
435 12
970 16
186 15
14 18 11
17 13
465 15
920 11
318 12
16 9 13
14 10
491 14
378 18
506 11
12 16 15
17 13
493 16
621 9
508 13
15 11 12
14 10
506 11
378 18
491 14
12 16 15
13 17
508 13
621 9
493 16
15 11 12
10 14
534 12
079 16
681 15
11 18 14
13 17
564 15
029 11
813 12
13 9 16
10 14
605 11
873 18
194 14
15 16 12
17 13
681 15
079 16
534 12
11 18 14
17 13
805 13
126 9
394 16
12 11 15
14 10
813 12
029 11
564 15
13 9 16
14 10
The program was:
DECLARE SUB permute (a$)
OPEN "notmagic.txt" FOR OUTPUT AS #2
a$ = "0123456789": h$ = a$
FOR i = 1 TO 3628800
n1 = VAL(MID$(a$, 1, 1))
n2 = VAL(MID$(a$, 2, 1))
n3 = VAL(MID$(a$, 3, 1))
n4 = VAL(MID$(a$, 4, 1))
n5 = VAL(MID$(a$, 5, 1))
n6 = VAL(MID$(a$, 6, 1))
n7 = VAL(MID$(a$, 7, 1))
n8 = VAL(MID$(a$, 8, 1))
n9 = VAL(MID$(a$, 9, 1))
n10 = VAL(MID$(a$, 10, 1))
t1 = n1 + n4 + n7
t2 = n2 + n5 + n8
t3 = n3 + n6 + n9
t4 = n1 + n2 + n3
t5 = n4 + n5 + n6
t6 = n7 + n8 + n9
t7 = n1 + n5 + n9
t8 = n3 + n5 + n7
REDIM diff(-7 TO 7)
diff(0) = 1
d = t2 - t1
IF ABS(d) < 8 THEN
IF diff(d) THEN GOTO nextPerm
diff(d) = 1
minDiff = d: maxDiff = d
ELSE
GOTO nextPerm
END IF
d = t3 - t1
IF ABS(d) < 8 THEN
IF diff(d) THEN GOTO nextPerm
diff(d) = 1
IF d < minDiff THEN minDiff = d
IF d > maxDiff THEN maxDiff = d
ELSE
GOTO nextPerm
END IF
d = t4 - t1
IF ABS(d) < 8 THEN
IF diff(d) THEN GOTO nextPerm
diff(d) = 1
IF d < minDiff THEN minDiff = d
IF d > maxDiff THEN maxDiff = d
ELSE
GOTO nextPerm
END IF
d = t5 - t1
IF ABS(d) < 8 THEN
IF diff(d) THEN GOTO nextPerm
diff(d) = 1
IF d < minDiff THEN minDiff = d
IF d > maxDiff THEN maxDiff = d
ELSE
GOTO nextPerm
END IF
d = t6 - t1
IF ABS(d) < 8 THEN
IF diff(d) THEN GOTO nextPerm
diff(d) = 1
IF d < minDiff THEN minDiff = d
IF d > maxDiff THEN maxDiff = d
ELSE
GOTO nextPerm
END IF
d = t7 - t1
IF ABS(d) < 8 THEN
IF diff(d) THEN GOTO nextPerm
diff(d) = 1
IF d < minDiff THEN minDiff = d
IF d > maxDiff THEN maxDiff = d
ELSE
GOTO nextPerm
END IF
d = t8 - t1
IF ABS(d) < 8 THEN
IF diff(d) THEN GOTO nextPerm
diff(d) = 1
IF d < minDiff THEN minDiff = d
IF d > maxDiff THEN maxDiff = d
ELSE
GOTO nextPerm
END IF
IF maxDiff - minDiff = 7 OR maxDiff - minDiff = 6 AND minDiff = 1 OR maxDiff = -1 THEN
PRINT #2, MID$(a$, 1, 3), t4
PRINT #2, MID$(a$, 4, 3), t5
PRINT #2, MID$(a$, 7, 3), t6
PRINT #2, t1, t2, t3
PRINT #2, , , , t7, t8
PRINT #2,
END IF
nextPerm:
permute a$
IF LEFT$(a$, 2) <> pVal$ THEN pVal$ = LEFT$(a$, 2): PRINT pVal$
NEXT
CLOSE
SUB permute (a$)
DEFINT A-Z
x$ = ""
FOR i = LEN(a$) TO 1 STEP -1
l$ = x$
x$ = MID$(a$, i, 1)
IF x$ < l$ THEN EXIT FOR
NEXT
IF i = 0 THEN
FOR j = 1 TO LEN(a$) \ 2
x$ = MID$(a$, j, 1)
MID$(a$, j, 1) = MID$(a$, LEN(a$) - j + 1, 1)
MID$(a$, LEN(a$) - j + 1, 1) = x$
NEXT
ELSE
FOR j = LEN(a$) TO i + 1 STEP -1
IF MID$(a$, j, 1) > x$ THEN EXIT FOR
NEXT
MID$(a$, i, 1) = MID$(a$, j, 1)
MID$(a$, j, 1) = x$
FOR j = 1 TO (LEN(a$) - i) \ 2
x$ = MID$(a$, i + j, 1)
MID$(a$, i + j, 1) = MID$(a$, LEN(a$) - j + 1, 1)
MID$(a$, LEN(a$) - j + 1, 1) = x$
NEXT
END IF
END SUB
And took 5 minutes to find all the solutions.
|
Posted by Charlie
on 2004-06-19 17:25:43 |