N is a 3x3 square grid which is constituted by using each of the digits from 1 to 9 exactly once.
Determine the probability that the first digit minus the second digit plus the third digit in each row (reading left to right), each column (reading top to bottom), and each main diagonal (reading top to bottom) of N is the same.
DECLARE SUB permute (a$)
CLS
a$ = "123456789": h$ = a$
DO
FOR i = 1 TO 9
v = VAL(MID$(a$, i, 1))
r = (i - 1) 3 + 1
c = (i - 1) MOD 3 + 1
b(r, c) = v
NEXT
good = 1
FOR r = 1 TO 3
v = b(r, 1) - b(r, 2) + b(r, 3)
IF r = 1 THEN
setV = v
ELSE
IF v <> setV THEN good = 0
END IF
v = b(1, r) - b(2, r) + b(3, r)
IF v <> setV THEN good = 0
NEXT
IF good THEN
IF b(1, 1) - b(2, 2) + b(3, 3) <> setV THEN good = 0
IF b(3, 1) - b(2, 2) + b(1, 3) <> setV THEN good = 0
END IF
IF good THEN
r = (solct 20) * 5 + 1: c = (solct MOD 20) * 4 + 1
LOCATE r, c: PRINT LEFT$(a$, 3)
LOCATE r + 1, c: PRINT MID$(a$, 4, 3)
LOCATE r + 2, c: PRINT RIGHT$(a$, 3);
LOCATE r + 3, c: PRINT setV;
solct = solct + 1
END IF
permute a$
LOOP UNTIL a$ = h$
PRINT : PRINT
PRINT solct
214 236 412 478 632 698 874 896
357 159 753 159 951 357 951 753
698 478 896 236 874 214 632 412
5 5 5 5 5 5 5 5
8
Giving, in agreement with Daniel, 8/9! = 1/45360 ~= 0.0000220459.
Edited on December 5, 2009, 2:36 pm
|
Posted by Charlie
on 2009-12-05 14:34:04 |