Arrange the numbers 1 to 15 inclusively in a straight line in such a way that any two adjacent numbers add up to a perfect square.
*** Disregard reflections.
(In reply to
P 'n P and then ... by brianjn)
DECLARE SUB addOn (n!)
CLEAR , , 25000
PRINT
DIM SHARED h(15), used(15)
FOR first = 1 TO 14
h(1) = first
used(first) = 1
addOn 2
used(first) = 0
NEXT
SUB addOn (n)
FOR new = 1 TO 15
IF used(new) = 0 THEN
pair = h(n - 1) + new
sr = INT(SQR(pair) + .5)
IF sr * sr = pair THEN
h(n) = new
used(new) = 1
IF n = 15 THEN
IF h(15) > h(1) THEN
FOR i = 1 TO 15
PRINT USING "###"; h(i);
NEXT: PRINT
PRINT " ";
FOR i = 1 TO 14
PRINT USING "###"; h(i) + h(i + 1);
NEXT: PRINT
END IF
ELSE
addOn n + 1
END IF
used(new) = 0
END IF
END IF
NEXT
END SUB
This also prevents reflections by requiring the last number to be higher than the first in the list in order to be printed out.
The results show the sequence as well as the pattern in the sums of pairs:
8 1 15 10 6 3 13 12 4 5 11 14 2 7 9
9 16 25 16 9 16 25 16 9 16 25 16 9 16
|
Posted by Charlie
on 2013-05-21 09:54:58 |