In a 4x4 magic square all rows, columns and the two main diagonals are distinct 4-digit prime numbers when read in either direction.
Create such a square.
Bonus (d4): same for order-5 magic square and 5-digit primes.
Based on
L. E. Card,Patterns in Primes, JRM 1:2, 1968, pp .93-99,
Although the program listing below found 8 solutions they are actually the same as they are manipulations of one unique solution.
1 1 9 3
9 2 5 7
3 8 5 1
3 3 1 9
1 9 3 3
1 2 8 3
9 5 5 1
3 7 1 9
3 3 1 9
3 8 5 1
9 2 5 7
1 1 9 3
3 3 9 1
3 8 2 1
1 5 5 9
9 1 7 3
3 7 1 9
9 5 5 1
1 2 8 3
1 9 3 3
3 9 1 1
7 5 2 9
1 5 8 3
9 1 3 3
9 1 3 3
1 5 8 3
7 5 2 9
3 9 1 1
9 1 7 3
1 5 5 9
3 8 2 1
3 3 9 1
The program recognises that the outer cells of the 4x4 must be 1,3,7 or 9 thus values are STEPped by 2 and values of 5 are redefined.
For each of 4 values per row, column and diagonal a 4 digit numeral is created and then those same values are used to create one in the reverse order. If those two numerals are not the same they are tested for primacy.
Ultimately if 20 unique numerals are generated their values are recorded as a 4x4 grid.
This listing has been edited. Due to the nesting depth this will only run under the QB64 environment where it is automatically compiled as a C++ EXE program before being executed. This particular program had a runtime of about 8 minutes.
DIM SHARED prm
DIM SHARED chk
prm = 0
OPEN "c:\qb64\work\reverse.txt" FOR OUTPUT AS #1
FOR a = 1 TO 9 STEP 2
IF a = 5 THEN a = 7
FOR b = 1 TO 9 STEP 2
IF b = 5 THEN b = 7
FOR c = 1 TO 9 STEP 2
IF c = 5 THEN c = 7
FOR d = 1 TO 9 STEP 2
IF d = 5 THEN d = 7
pr1 = a * 1000 + b * 100 + c * 10 + d
pr2 = a + b * 10 + c * 100 + d * 1000
IF pr1 <> pr2 THEN
chk = pr1
primetest
IF prm = 0 THEN
chk = pr2
primetest
IF prm = 0 THEN
FOR e = 1 TO 9 STEP 2
IF e = 5 THEN e = 7
FOR i = 1 TO 9
IF i = 5 THEN i = 7
FOR m = 1 TO 9 STEP 2
IF m = 5 THEN m = 7
pr3 = a * 1000 + e * 100 + i * 10 + m
pr4 = a + e * 10 + i * 100 + m * 1000
IF pr3 <> pr4 THEN
chk = pr3
primetest
IF prm = 0 THEN
chk = pr4
primetest
IF prm = 0 THEN
FOR f = 0 TO 9
FOR k = 0 TO 9
FOR p = 1 TO 9 STEP 2
IF p = 5 THEN p = 7
pr5 = a * 1000 + f * 100 + k * 10 + p
pr6 = a + f * 10 + k * 100 + p * 1000
IF pr5 <> pr6 THEN
chk = pr5
primetest
IF prm = 0 THEN
chk = pr6
primetest
IF prm = 0 THEN
FOR g = 0 TO 9
FOR h = 1 TO 9 STEP 2
IF h = 5 THEN h = 7
pr7 = e * 1000 + f * 100 + g * 10 + h
pr8 = e + f * 10 + g * 100 + h * 1000
IF pr7 <> pr8 THEN
chk = pr7
primetest
IF prm = 0 THEN
chk = pr8
primetest
IF prm = 0 THEN
FOR j = 0 TO 9
FOR l = 1 TO 9 STEP 2
IF l = 5 THEN l = 7
pr9 = i * 1000 + j * 100 + k * 10 + l
pr10 = i + j * 10 + k * 100 + l * 1000
IF pr9 <> pr10 THEN
chk = pr9
primetest
IF prm = 0 THEN
chk = pr10
primetest
IF prm = 0 THEN
FOR n = 1 TO 9 STEP 2
IF n = 5 THEN n = 7
FOR o = 1 TO 9 STEP 2
IF o = 5 THEN o = 7
pr11 = m * 1000 + n * 100 + o * 10 + p
pr12 = m + n * 10 + o * 100 + p * 1000
IF pr11 <> pr12 THEN
chk = pr11
primetest
IF prm = 0 THEN
chk = pr12
primetest
IF prm = 0 THEN
pr13 = b * 1000 + f * 100 + j * 10 + n
pr14 = b + f * 10 + j * 100 + n * 1000
IF pr13 <> pr14 THEN
chk = pr13
primetest
IF prm = 0 THEN
chk = pr14
primetest
IF prm = 0 THEN
pr15 = c * 1000 + g * 100 + k * 10 + o
pr16 = c + g * 10 + k * 100 + o * 1000
IF pr15 <> pr16 THEN
chk = pr15
primetest
IF prm = 0 THEN
chk = pr16
primetest
IF prm = 0 THEN
pr17 = d * 1000 + h * 100 + l * 10 + p
pr18 = d + h * 10 + l * 100 + p * 1000
IF pr17 <> pr18 THEN
chk = pr17
primetest
IF prm = 0 THEN
chk = pr18
primetest
IF prm = 0 THEN
pr19 = d * 1000 + g * 100 + j * 10 + m
pr20 = d + g * 10 + j * 100 + m * 1000
IF pr19 <> pr20 THEN
chk = pr19
primetest
IF prm = 0 THEN
chk = pr20
primetest
IF prm = 0 THEN
IF pr1 <> pr2 AND .............pr1 <> pr19 AND pr1 <> pr20 THEN
IF pr2 <> pr3 AND .............pr2 <> pr19 AND pr2 <> pr20 THEN
...............
...............(Note that every numeral is compared
............... with every other )
...............
IF pr18 <> pr19 AND pr18 <> pr20 THEN
IF pr19 <> pr20 THEN
LOCATE 1, 1: PRINT " a"; " b"; " c"; " d"; " e"; " f"; " g"; " h"; " i"; " j"; " k"; " l"; " m"; " n"; " o"; " p"
LOCATE 2, 1: PRINT a; b; c; d; e; f; g; h; i; j; k; l; m; n; o; p
(Display grid, one on screen and one to file) LOCATE 3, 1: PRINT a; b; c; d
LOCATE 4, 1: PRINT e; f; g; h
LOCATE 5, 1: PRINT i; j; k; l
LOCATE 6, 1: PRINT m; n; o; p
PRINT #1, a; b; c; d
PRINT #1, e; f; g; h
PRINT #1, i; j; k; l
PRINT #1, m; n; o; p
PRINT #1,
PRINT #1,
....................
END IFs and NEXTs to close nested IF and FOR
....................
CLOSE 1
SUB primetest
prm = 0
s = 2
DO
IF (chk MOD s = 0) THEN prm = 1
s = s + 1
LOOP WHILE (s < chk AND prm = 0)
END SUB
|
Posted by brianjn
on 2012-01-14 20:06:09 |