' Generates Concentric Magic Squares of order 8 for Prime Numbers
' Optimized for Twin Squares
' Tested with Office 2007 under Windows 7
Sub Priem8a2()
Dim a1(2448), a(64), a6(64), b1(105769), b(105769), c(64)
y = MsgBox("Locked", vbCritical, "Routine Priem8a2")
End
n2 = 0: n3 = 0: n9 = 0: k1 = 1: k2 = 1
Sht1 = "Pairs6a"
Sht2 = "Lines6"
Sheets("Klad1").Select
t1 = Timer
For j100 = 85 To 95 'Restart from 85
Rcrd1a = Sheets(Sht2).Cells(j100, 38).Value
' Define variables
Pr8 = Sheets(Sht1).Cells(Rcrd1a, 1).Value 'Pair Sum
s1 = 4 * Pr8 'MC8
nvar = Sheets(Sht1).Cells(Rcrd1a, 5).Value
m1 = 1: m2 = nvar
If nvar < 64 Then GoTo 1000
Cells(k1, 1).Select: Cells(k1, 1).Value = j100
For i1 = m1 To m2
a1(i1) = Sheets(Sht1).Cells(Rcrd1a, i1 + 10).Value
Next i1
If a1(1) = 1 Then m1 = m1 + 1: m2 = m2 - 1
pMax = a1(m2)
Erase b1
For i1 = m1 To m2
b1(a1(i1)) = a1(i1)
Next i1
' Read Center Square and remove from Available Primes
' Including Complements for Non Symmetrical Center Squares
For i1 = 1 To 36
a6(i1) = Sheets(Sht2).Cells(j100, i1).Value
b1(a6(i1)) = 0
b1(Pr8 - a6(i1)) = 0
Next i1
Erase a
' Load Center Square
a(10) = a6(1): a(11) = a6(2): a(12) = a6(3): a(13) = a6(4): a(14) = a6(5): a(15) = a6(6):
a(18) = a6(7): a(19) = a6(8): a(20) = a6(9): a(21) = a6(10): a(22) = a6(11): a(23) = a6(12):
a(26) = a6(13): a(27) = a6(14): a(28) = a6(15): a(29) = a6(16): a(30) = a6(17): a(31) = a6(18):
a(34) = a6(19): a(35) = a6(20): a(36) = a6(21): a(37) = a6(22): a(38) = a6(23): a(39) = a6(24):
a(42) = a6(25): a(43) = a6(26): a(44) = a6(27): a(45) = a6(28): a(46) = a6(29): a(47) = a6(30):
a(50) = a6(31): a(51) = a6(32): a(52) = a6(33): a(53) = a6(34): a(54) = a6(35): a(55) = a6(36):
Erase a6
' Restore available pairs in a1()
n10 = 0
For j1 = 1 To pMax
If b1(j1) <> 0 Then
n10 = n10 + 1
a1(n10) = b1(j1)
End If
Next j1
m1 = 1: m2 = n10: n10 = 0
If a1(1) = 1 Then m1 = 2: m2 = m2 - 1
' Determine Border
t11 = Timer 'Time Out
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)
t12 = Timer: t13 = t12 - t11 'Time Out
If t13 > 100 Then Erase b1, b, c: t11 = Timer: GoTo 1000 'Time Out, Try Next j100
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 Priem8a2")
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 = 3 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