Arrange the digits from 1 to 9 in a 3 x 3 array in such a way that the sum of a number's immediate neighbors (including diagonals) is a multiple of that number. (Disregard rotations and reflections.)
DECLARE SUB permute (a$)
CLS
a$ = "123456789": h$ = a$
DO
IF MID$(a$, 1, 1) < MID$(a$, 3, 1) AND MID$(a$, 1, 1) < MID$(a$, 7, 1) AND MID$(a$, 1, 1) < MID$(a$, 9, 1) THEN
IF MID$(a$, 3, 1) < MID$(a$, 7, 1) THEN
grid(1, 1) = VAL(MID$(a$, 1, 1))
grid(1, 2) = VAL(MID$(a$, 2, 1))
grid(1, 3) = VAL(MID$(a$, 3, 1))
grid(2, 1) = VAL(MID$(a$, 4, 1))
grid(2, 2) = VAL(MID$(a$, 5, 1))
grid(2, 3) = VAL(MID$(a$, 6, 1))
grid(3, 1) = VAL(MID$(a$, 7, 1))
grid(3, 2) = VAL(MID$(a$, 8, 1))
grid(3, 3) = VAL(MID$(a$, 9, 1))
good = 1
FOR row = 1 TO 3
FOR col = 1 TO 3
tot = 0
FOR r = row - 1 TO row + 1
FOR c = col - 1 TO col + 1
tot = tot + grid(r, c)
NEXT
NEXT
IF tot MOD grid(row, col) <> 0 THEN good = 0: EXIT FOR
NEXT
IF good = 0 THEN EXIT FOR
NEXT
IF good THEN
PRINT MID$(a$, 1, 3)
PRINT MID$(a$, 4, 3)
PRINT MID$(a$, 7, 3)
PRINT
END IF
END IF
END IF
permute a$
LOOP UNTIL a$ = h$
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
finds
265
731
984
|
Posted by Charlie
on 2013-12-19 19:07:41 |