' Generates Concentric Magic Squares of order 8 for Prime Numbers
' Tested with Office 2007 under Windows 7
Sub Priem8a()
Dim a1(292), a(64), a2(36), b1(1585), b(1585), c(64)
y = MsgBox("Locked", vbCritical, "Routine Priem8a")
End
Sheets("Klad1").Select
n5 = 0: n9 = 0: k1 = 1: k2 = 1
Sht1 = "Pairs": Sht2 = "Solutions1443"
t1 = Timer
For j100 = 2 To 113
' Read Pairs
s1 = Sheets(Sht1).Cells(j100, 4).Value
nVar = Sheets(Sht1).Cells(j100, 5).Value
If nVar < 70 Then GoTo 1000
Erase b1
For j1 = 1 To nVar
x = Sheets(Sht1).Cells(j100, j1 + 6).Value
b1(x) = x
Next j1
a20 = Sheets(Sht1).Cells(j100, nVar + 6).Value 'Maximum Available Prime
' Read Magic Square 6 x 6
j200 = j100 - 1
n3 = Int(j200 / 4)
n4 = (j200 / 4 - n3) * 4
If n4 = 0 Then n4 = 4: n3 = n3 - 1
n1 = n3 * 7 + 2
n2 = (n4 - 1) * 7 + 2
i4 = 0
For j1 = n1 To n1 + 5 'Row within square j200
For j2 = n2 To n2 + 5 'Column within square j200
i4 = i4 + 1
a2(i4) = Sheets(Sht2).Cells(j1, j2).Value 'load Magic Square 6 x 6
Next j2
Next j1
' Store Magic Square 6 x 6 in a()
a(10) = a2(1): a(11) = a2(2): a(12) = a2(3): a(13) = a2(4): a(14) = a2(5): a(15) = a2(6):
a(18) = a2(7): a(19) = a2(8): a(20) = a2(9): a(21) = a2(10): a(22) = a2(11): a(23) = a2(12):
a(26) = a2(13): a(27) = a2(14): a(28) = a2(15): a(29) = a2(16): a(30) = a2(17): a(31) = a2(18):
a(34) = a2(19): a(35) = a2(20): a(36) = a2(21): a(37) = a2(22): a(38) = a2(23): a(39) = a2(24):
a(42) = a2(25): a(43) = a2(26): a(44) = a2(27): a(45) = a2(28): a(46) = a2(29): a(47) = a2(30):
a(50) = a2(31): a(51) = a2(32): a(52) = a2(33): a(53) = a2(34): a(54) = a2(35): a(55) = a2(36):
' Remove used pairs from b1()
For j1 = 1 To 36
b1(a2(j1)) = 0
Next j1
' Restore available pairs in a1()
n10 = 0
For j1 = 1 To a20
If b1(j1) <> 0 Then
n10 = n10 + 1
a1(n10) = b1(j1)
End If
Next j1
m1 = 1: m2 = n10
' Determine Border
For j64 = m1 To m2 'a(64)
If b(a1(j64)) = 0 Then b(a1(j64)) = a1(j64): c(64) = a1(j64) Else GoTo 640
a(64) = a1(j64)
a(1) = s1 / 4 - a(64): If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 10
For j63 = m1 To m2 'a(63)
If b(a1(j63)) = 0 Then b(a1(j63)) = a1(j63): c(63) = a1(j63) Else GoTo 630
a(63) = a1(j63)
a(7) = s1 / 4 - a(63): If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 70
For j62 = m1 To m2 'a(62)
If b(a1(j62)) = 0 Then b(a1(j62)) = a1(j62): c(62) = a1(j62) Else GoTo 620
a(62) = a1(j62)
a(6) = s1 / 4 - a(62): If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 60
For j61 = m1 To m2 'a(61)
If b(a1(j61)) = 0 Then b(a1(j61)) = a1(j61): c(61) = a1(j61) Else GoTo 610
a(61) = a1(j61)
a(5) = s1 / 4 - a(61): If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 50
For j60 = m1 To m2 'a(60)
If b(a1(j60)) = 0 Then b(a1(j60)) = a1(j60): c(60) = a1(j60) Else GoTo 600
a(60) = a1(j60)
a(4) = s1 / 4 - a(60): If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 40
For j59 = m1 To m2 'a(59)
If b(a1(j59)) = 0 Then b(a1(j59)) = a1(j59): c(59) = a1(j59) Else GoTo 590
a(59) = a1(j59)
a(3) = s1 / 4 - a(59): If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 30
For j58 = m1 To m2 'a(58)
If b(a1(j58)) = 0 Then b(a1(j58)) = a1(j58): c(58) = a1(j58) Else GoTo 580
a(58) = a1(j58)
a(57) = s1 - a(58) - a(59) - a(60) - a(61) - a(62) - a(63) - a(64)
If a(57) < a1(m1) Or a(57) > a1(m2) Then GoTo 570
If b1(a(57)) = 0 Then GoTo 570
If b(a(57)) = 0 Then b(a(57)) = a(57): c(57) = a(57) Else GoTo 570
a(8) = s1 / 4 - a(57): If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 80
a(2) = s1 / 4 - a(58): If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 20
For j56 = m1 To m2 'a(56)
If b(a1(j56)) = 0 Then b(a1(j56)) = a1(j56): c(56) = a1(j56) Else GoTo 560
a(56) = a1(j56)
a(49) = s1 / 4 - a(56): If b(a(49)) = 0 Then b(a(49)) = a(49): c(49) = a(49) Else GoTo 490
For j48 = m1 To m2 'a(48)
If b(a1(j48)) = 0 Then b(a1(j48)) = a1(j48): c(48) = a1(j48) Else GoTo 480
a(48) = a1(j48)
a(41) = s1 / 4 - a(48): If b(a(41)) = 0 Then b(a(41)) = a(41): c(41) = a(41) Else GoTo 410
For j40 = m1 To m2 'a(40)
If b(a1(j40)) = 0 Then b(a1(j40)) = a1(j40): c(40) = a1(j40) Else GoTo 400
a(40) = a1(j40)
a(33) = s1 / 4 - a(40): If b(a(33)) = 0 Then b(a(33)) = a(33): c(33) = a(33) Else GoTo 330
For j32 = m1 To m2 'a(32)
If b(a1(j32)) = 0 Then b(a1(j32)) = a1(j32): c(32) = a1(j32) Else GoTo 320
a(32) = a1(j32)
a(25) = s1 / 4 - a(32): If b(a(25)) = 0 Then b(a(25)) = a(25): c(25) = a(25) Else GoTo 250
For j24 = m1 To m2 'a(24)
If b(a1(j24)) = 0 Then b(a1(j24)) = a1(j24): c(24) = a1(j24) Else GoTo 240
a(24) = a1(j24)
a(17) = s1 / 4 - a(24): If b(a(17)) = 0 Then b(a(17)) = a(17): c(17) = a(17) Else GoTo 170
a(16) = s1 - a(8) - a(24) - a(32) - a(40) - a(48) - a(56) - a(64)
If a(16) < a1(m1) Or a(16) > a1(m2) Then GoTo 160
If b1(a(16)) = 0 Then GoTo 160
If b(a(16)) = 0 Then b(a(16)) = a(16): c(16) = a(16) Else GoTo 160
a(9) = s1 / 4 - a(16): If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 90
n9 = n9 + 1
GoSub 2650 'Print results (squares)
' GoSub 2645 'Print results (selected numbers)
Erase b, c: GoTo 1000 'Print only first square
b(c(9)) = 0: c(0) = 0
90 b(c(16)) = 0: c(16) = 0
160 b(c(17)) = 0: c(17) = 0
170 b(c(24)) = 0: c(24) = 0
240 Next j24
b(c(25)) = 0: c(25) = 0
250 b(c(32)) = 0: c(32) = 0
320 Next j32
b(c(33)) = 0: c(33) = 0
330 b(c(40)) = 0: c(40) = 0
400 Next j40
b(c(41)) = 0: c(41) = 0
410 b(c(48)) = 0: c(48) = 0
480 Next j48
b(c(49)) = 0: c(49) = 0
490 b(c(56)) = 0: c(56) = 0
560 Next j56
b(c(2)) = 0: c(2) = 0
20 b(c(8)) = 0: c(8) = 0
80 b(c(57)) = 0: c(57) = 0
570 b(c(58)) = 0: c(58) = 0
580 Next j58
b(c(3)) = 0: c(3) = 0
30 b(c(59)) = 0: c(59) = 0
590 Next j59
b(c(4)) = 0: c(4) = 0
40 b(c(60)) = 0: c(60) = 0
600 Next j60
b(c(5)) = 0: c(5) = 0
50 b(c(61)) = 0: c(61) = 0
610 Next j61
b(c(6)) = 0: c(6) = 0
60 b(c(62)) = 0: c(62) = 0
620 Next j62
b(c(7)) = 0: c(7) = 0
70 b(c(63)) = 0: c(63) = 0
630 Next j63
b(c(1)) = 0: c(1) = 0
10 b(c(64)) = 0: c(64) = 0
640 Next j64
Erase b, c
1000 Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Priem8a")
End
' Print results (selected numbers)
2645 For i1 = 1 To 64
Cells(n9, i1).Value = a(i1)
Next i1
Return
' Print results (squares)
2650 n5 = n5 + 1
If n5 = 5 Then
n5 = 1: k1 = k1 + 9: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 9
End If
Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = "MC = " + CStr(s1)
i3 = 0
For i1 = 1 To 8
For i2 = 1 To 8
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a(i3)
Next i2
Next i1
Return
End Sub