All the nine digits are arranged here so as to form four square numbers.
9, 81, 324, 576
Which is the single smallest possible square number and a single largest possible square number using all the 9 digits exactly once?
What are the possible two, three & four number sets that follow this logic?
These are all the perfect squares using all 9 digits 1-9 exactly once, with no zeros:
139854276
152843769
157326849
215384976
245893761
254817369
326597184
361874529
375468129
382945761
385297641
412739856
523814769
529874361
537219684
549386721
587432169
589324176
597362481
615387249
627953481
653927184
672935481
697435281
714653289
735982641
743816529
842973156
847159236
923187456
There are 30 of them.
The smallest is 139854276
The largest is 923187456
All pairs of squares of the sort are:
324 751689
3249 15876
36 5184729
36 5948721
4356 71289
576 321489
576 349281
576 381924
729 385641
81 2537649
81 5673924
81 7436529
81 9253764
8649 35721
9 13527684
9 34857216
9 65318724
9 73256481
9 81432576
The triplets are:
1 256 73984
1 4 3297856
1 4 3857296
1 4 5827396
1 4 6385729
1 4 8567329
1 4 9572836
1 49 872356
1 625 73984
16 25 73984
1 64 537289
16 784 5329
25 784 1369
25 784 1936
25 841 7396
361 529 784
36 729 5184
36 81 74529
36 81 79524
4 16 537289
4 25 139876
4 25 391876
4 289 15376
81 324 7569
81 576 3249
81 729 4356
9 324 15876
The quadruplets:
1 36 529 784
1 4 9 872356
4 25 81 7396
9 25 361 784
9 81 324 576
... and theres a set of five:
1 9 25 36 784
There is no set of six.
DECLARE SUB test3 (s$, s1#, s2#, s3#)
DECLARE SUB test4 (s$, s1#, s2#, s3#, s4#)
DECLARE SUB test5 (s$, s1#, s2#, s3#, s4#, s5#)
DECLARE SUB test6 (s$, s1#, s2#, s3#, s4#, s5#, s6#)
DECLARE FUNCTION isSq# (x#)
DECLARE SUB permute (a$)
DEFDBL A-Z
OPEN "digandsq.txt" FOR OUTPUT AS #2
large$ = " ": small$ = "z"
FOR i = 11111 TO 31427
n$ = LTRIM$(STR$(i * i))
good = 1
IF INSTR(n$, "0") THEN
good = 0
ELSE
FOR j = 1 TO 8
IF INSTR(MID$(n$, j + 1), MID$(n$, j, 1)) THEN good = 0
NEXT
END IF
IF good THEN
PRINT #2, n$: sCt = sCt + 1
IF n$ > large$ THEN large$ = n$
IF n$ < small$ THEN small$ = n$
END IF
NEXT
PRINT #2, sCt: PRINT #2, small$: PRINT #2, large$
PRINT #2,
n$ = "123456789"
h$ = n$
DO
n1 = VAL(LEFT$(n$, 1)): n2 = VAL(MID$(n$, 2))
IF isSq(n1) AND isSq(n2) THEN PRINT #2, n1; n2
n1 = VAL(LEFT$(n$, 2)): n2 = VAL(MID$(n$, 3))
IF isSq(n1) AND isSq(n2) THEN PRINT #2, n1; n2
n1 = VAL(LEFT$(n$, 3)): n2 = VAL(MID$(n$, 4))
IF isSq(n1) AND isSq(n2) THEN PRINT #2, n1; n2
n1 = VAL(LEFT$(n$, 4)): n2 = VAL(MID$(n$, 5))
IF isSq(n1) AND isSq(n2) THEN PRINT #2, n1; n2
permute n$
LOOP UNTIL n$ = h$
DO
test3 n$, 1, 1, 7
test3 n$, 1, 2, 6
test3 n$, 1, 3, 5
test3 n$, 1, 4, 4
test3 n$, 2, 2, 5
test3 n$, 2, 3, 4
test3 n$, 3, 3, 3
permute n$
LOOP UNTIL n$ = h$
testing4:
DO
test4 n$, 1, 1, 1, 6
test4 n$, 1, 1, 2, 5
test4 n$, 1, 1, 3, 4
test4 n$, 1, 2, 2, 4
test4 n$, 1, 2, 3, 3
test4 n$, 2, 2, 2, 3
permute n$
LOOP UNTIL n$ = h$
testing5:
DO
test5 n$, 1, 1, 1, 2, 4
test5 n$, 1, 1, 1, 3, 3
test5 n$, 1, 1, 2, 2, 3
test5 n$, 1, 2, 2, 2, 2
permute n$
LOOP UNTIL n$ = h$
testing6:
DO
test6 n$, 1, 1, 1, 2, 2, 2
permute n$
LOOP UNTIL n$ = h$
CLOSE
FUNCTION isSq (x)
t = INT(SQR(x) + .5)
IF t * t = x THEN isSq = 1: ELSE isSq = 0
END FUNCTION
DEFDBL A-Z
SUB test3 (s$, s1, s2, s3)
n1 = VAL(LEFT$(s$, s1))
n2 = VAL(MID$(s$, s1 + 1, s2))
n3 = VAL(MID$(s$, s1 + s2 + 1, s3))
IF n1 < n2 AND n2 < n3 THEN
IF isSq(n1) AND isSq(n2) AND isSq(n3) THEN
PRINT #2, n1; n2; n3
END IF
END IF
END SUB
SUB test4 (s$, s1, s2, s3, s4)
n1 = VAL(LEFT$(s$, s1))
n2 = VAL(MID$(s$, s1 + 1, s2))
n3 = VAL(MID$(s$, s1 + s2 + 1, s3))
n4 = VAL(MID$(s$, s1 + s2 + s3 + 1, s4))
IF n1 < n2 AND n2 < n3 AND n3 < n4 THEN
IF isSq(n1) AND isSq(n2) AND isSq(n3) AND isSq(n4) THEN
PRINT #2, n1; n2; n3; n4
END IF
END IF
END SUB
SUB test5 (s$, s1, s2, s3, s4, s5)
n1 = VAL(LEFT$(s$, s1))
n2 = VAL(MID$(s$, s1 + 1, s2))
n3 = VAL(MID$(s$, s1 + s2 + 1, s3))
n4 = VAL(MID$(s$, s1 + s2 + s3 + 1, s4))
n5 = VAL(MID$(s$, s1 + s2 + s3 + s4 + 1, s5))
IF n1 < n2 AND n2 < n3 AND n3 < n4 AND n4 < n5 THEN
IF isSq(n1) AND isSq(n2) AND isSq(n3) AND isSq(n4) AND isSq(n5) THEN
PRINT #2, n1; n2; n3; n4; n5
END IF
END IF
END SUB
SUB test6 (s$, s1, s2, s3, s4, s5, s6)
n1 = VAL(LEFT$(s$, s1))
n2 = VAL(MID$(s$, s1 + 1, s2))
n3 = VAL(MID$(s$, s1 + s2 + 1, s3))
n4 = VAL(MID$(s$, s1 + s2 + s3 + 1, s4))
n5 = VAL(MID$(s$, s1 + s2 + s3 + s4 + 1, s5))
n6 = VAL(MID$(s$, s1 + s2 + s3 + s4 + s5 + 1, s5))
IF n1 < n2 AND n2 < n3 AND n3 < n4 AND n4 < n5 AND n5 < n6 THEN
IF isSq(n1) AND isSq(n2) AND isSq(n3) AND isSq(n4) AND isSq(n5) AND isSq(n6) THEN
PRINT #2, n1; n2; n3; n4; n5; n6
END IF
END IF
END SUB
The permute subroutine is shown elsewhere on the site.
|
Posted by Charlie
on 2006-10-12 11:35:28 |