' Constructs Prime Number Magic Squares of order 8
' Tested with Office 2007 under Windows 7
Sub CnstrSqrs8b()
Dim a2(8), b2(8), a(64), b(64), c(64)
y = MsgBox("Locked", vbExclamation, "Routine CnstrSqrs8b")
End
n2 = 0: n9 = 0: k1 = 1: k2 = 1
Sheets("Klad1").Select
t1 = Timer
For j1 = 2 To 7
' Read (Balanced) Magic Lines
For j2 = 1 To 8: a2(j2) = Sheets("Att83b").Cells(j1, j2).Value: Next j2
For j2 = 1 To 8: b2(j2) = Sheets("Att83b").Cells(j1, j2 + 9).Value: Next j2
s1 = Sheets("Att83b").Cells(j1, 21).Value
' Construct squares a() and b()
GoSub 200
' Calculate Square c()
For j2 = 1 To 64
c(j2) = a(j2) + b(j2)
Next j2
' Print results
GoSub 800: If fl1 = 0 Then GoTo 70
' n9 = n9 + 1: GoSub 640 'Lines
n9 = n9 + 1: GoSub 650 'Squares
70 Next j1
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s1)
y = MsgBox(t10, vbInformation, "Routine CnstrSqrs8b")
End
' Construct squares a() and b()
200
' Simple Magic
''a(1) = a2(1): a(2) = a2(2): a(3) = a2(3): a(4) = a2(4):
''a(5) = a2(5): a(6) = a2(6): a(7) = a2(7): a(8) = a2(8):
''a(9) = a2(5): a(10) = a2(6): a(11) = a2(7): a(12) = a2(8):
''a(13) = a2(1): a(14) = a2(2): a(15) = a2(3): a(16) = a2(4):
''a(17) = a2(4): a(18) = a2(3): a(19) = a2(2): a(20) = a2(1):
''a(21) = a2(8): a(22) = a2(7): a(23) = a2(6): a(24) = a2(5):
''a(25) = a2(8): a(26) = a2(7): a(27) = a2(6): a(28) = a2(5):
''a(29) = a2(4): a(30) = a2(3): a(31) = a2(2): a(32) = a2(1):
''a(33) = a2(7): a(34) = a2(8): a(35) = a2(5): a(36) = a2(6):
''a(37) = a2(3): a(38) = a2(4): a(39) = a2(1): a(40) = a2(2):
''a(41) = a2(3): a(42) = a2(4): a(43) = a2(1): a(44) = a2(2):
''a(45) = a2(7): a(46) = a2(8): a(47) = a2(5): a(48) = a2(6):
''a(49) = a2(6): a(50) = a2(5): a(51) = a2(8): a(52) = a2(7):
''a(53) = a2(2): a(54) = a2(1): a(55) = a2(4): a(56) = a2(3):
''a(57) = a2(2): a(58) = a2(1): a(59) = a2(4): a(60) = a2(3):
''a(61) = a2(6): a(62) = a2(5): a(63) = a2(8): a(64) = a2(7):
''b(1) = b2(1): b(2) = b2(2): b(3) = b2(3): b(4) = b2(4):
''b(5) = b2(5): b(6) = b2(6): b(7) = b2(7): b(8) = b2(8):
''b(9) = b2(3): b(10) = b2(4): b(11) = b2(1): b(12) = b2(2):
''b(13) = b2(7): b(14) = b2(8): b(15) = b2(5): b(16) = b2(6):
''b(17) = b2(5): b(18) = b2(6): b(19) = b2(7): b(20) = b2(8):
''b(21) = b2(1): b(22) = b2(2): b(23) = b2(3): b(24) = b2(4):
''b(25) = b2(7): b(26) = b2(8): b(27) = b2(5): b(28) = b2(6):
''b(29) = b2(3): b(30) = b2(4): b(31) = b2(1): b(32) = b2(2):
''b(33) = b2(6): b(34) = b2(5): b(35) = b2(8): b(36) = b2(7):
''b(37) = b2(2): b(38) = b2(1): b(39) = b2(4): b(40) = b2(3):
''b(41) = b2(8): b(42) = b2(7): b(43) = b2(6): b(44) = b2(5):
''b(45) = b2(4): b(46) = b2(3): b(47) = b2(2): b(48) = b2(1):
''b(49) = b2(2): b(50) = b2(1): b(51) = b2(4): b(52) = b2(3):
''b(53) = b2(6): b(54) = b2(5): b(55) = b2(8): b(56) = b2(7):
''b(57) = b2(4): b(58) = b2(3): b(59) = b2(2): b(60) = b2(1):
''b(61) = b2(8): b(62) = b2(7): b(63) = b2(6): b(64) = b2(5):
' Pan Magic (Most Perfect)
''a(1) = a2(1): a(2) = a2(2): a(3) = a2(3): a(4) = a2(4):
''a(5) = a2(8): a(6) = a2(7): a(7) = a2(6): a(8) = a2(5):
''a(9) = a2(8): a(10) = a2(7): a(11) = a2(6): a(12) = a2(5):
''a(13) = a2(1): a(14) = a2(2): a(15) = a2(3): a(16) = a2(4):
''a(17) = a2(1): a(18) = a2(2): a(19) = a2(3): a(20) = a2(4):
''a(21) = a2(8): a(22) = a2(7): a(23) = a2(6): a(24) = a2(5):
''a(25) = a2(8): a(26) = a2(7): a(27) = a2(6): a(28) = a2(5):
''a(29) = a2(1): a(30) = a2(2): a(31) = a2(3): a(32) = a2(4):
''a(33) = a2(1): a(34) = a2(2): a(35) = a2(3): a(36) = a2(4):
''a(37) = a2(8): a(38) = a2(7): a(39) = a2(6): a(40) = a2(5):
''a(41) = a2(8): a(42) = a2(7): a(43) = a2(6): a(44) = a2(5):
''a(45) = a2(1): a(46) = a2(2): a(47) = a2(3): a(48) = a2(4):
''a(49) = a2(1): a(50) = a2(2): a(51) = a2(3): a(52) = a2(4):
''a(53) = a2(8): a(54) = a2(7): a(55) = a2(6): a(56) = a2(5):
''a(57) = a2(8): a(58) = a2(7): a(59) = a2(6): a(60) = a2(5):
''a(61) = a2(1): a(62) = a2(2): a(63) = a2(3): a(64) = a2(4):
''b(1) = b2(1): b(2) = b2(8): b(3) = b2(1): b(4) = b2(8):
''b(5) = b2(1): b(6) = b2(8): b(7) = b2(1): b(8) = b2(8):
''b(9) = b2(2): b(10) = b2(7): b(11) = b2(2): b(12) = b2(7):
''b(13) = b2(2): b(14) = b2(7): b(15) = b2(2): b(16) = b2(7):
''b(17) = b2(3): b(18) = b2(6): b(19) = b2(3): b(20) = b2(6):
''b(21) = b2(3): b(22) = b2(6): b(23) = b2(3): b(24) = b2(6):
''b(25) = b2(4): b(26) = b2(5): b(27) = b2(4): b(28) = b2(5):
''b(29) = b2(4): b(30) = b2(5): b(31) = b2(4): b(32) = b2(5):
''b(33) = b2(8): b(34) = b2(1): b(35) = b2(8): b(36) = b2(1):
''b(37) = b2(8): b(38) = b2(1): b(39) = b2(8): b(40) = b2(1):
''b(41) = b2(7): b(42) = b2(2): b(43) = b2(7): b(44) = b2(2):
''b(45) = b2(7): b(46) = b2(2): b(47) = b2(7): b(48) = b2(2):
''b(49) = b2(6): b(50) = b2(3): b(51) = b2(6): b(52) = b2(3):
''b(53) = b2(6): b(54) = b2(3): b(55) = b2(6): b(56) = b2(3):
''b(57) = b2(5): b(58) = b2(4): b(59) = b2(5): b(60) = b2(4):
''b(61) = b2(5): b(62) = b2(4): b(63) = b2(5): b(64) = b2(4):
' Bordered, Center: Square of the Sun
''a(1) = a2(1): a(2) = a2(2): a(3) = a2(3): a(4) = a2(4):
''a(5) = a2(5): a(6) = a2(6): a(7) = a2(7): a(8) = a2(8):
''a(9) = a2(8): a(10) = a2(2): a(11) = a2(6): a(12) = a2(5):
''a(13) = a2(4): a(14) = a2(3): a(15) = a2(7): a(16) = a2(1):
''a(17) = a2(8): a(18) = a2(7): a(19) = a2(3): a(20) = a2(5):
''a(21) = a2(4): a(22) = a2(6): a(23) = a2(2): a(24) = a2(1):
''a(25) = a2(1): a(26) = a2(7): a(27) = a2(6): a(28) = a2(4):
''a(29) = a2(5): a(30) = a2(3): a(31) = a2(2): a(32) = a2(8):
''a(33) = a2(1): a(34) = a2(2): a(35) = a2(6): a(36) = a2(4):
''a(37) = a2(5): a(38) = a2(3): a(39) = a2(7): a(40) = a2(8):
''a(41) = a2(8): a(42) = a2(7): a(43) = a2(3): a(44) = a2(4):
''a(45) = a2(5): a(46) = a2(6): a(47) = a2(2): a(48) = a2(1):
''a(49) = a2(8): a(50) = a2(2): a(51) = a2(3): a(52) = a2(5):
''a(53) = a2(4): a(54) = a2(6): a(55) = a2(7): a(56) = a2(1):
''a(57) = a2(1): a(58) = a2(7): a(59) = a2(6): a(60) = a2(5):
''a(61) = a2(4): a(62) = a2(3): a(63) = a2(2): a(64) = a2(8):
''b(1) = b2(1): b(2) = b2(8): b(3) = b2(8): b(4) = b2(1):
''b(5) = b2(1): b(6) = b2(8): b(7) = b2(8): b(8) = b2(1):
''b(9) = b2(2): b(10) = b2(2): b(11) = b2(7): b(12) = b2(7):
''b(13) = b2(2): b(14) = b2(7): b(15) = b2(2): b(16) = b2(7):
''b(17) = b2(3): b(18) = b2(6): b(19) = b2(3): b(20) = b2(6):
''b(21) = b2(6): b(22) = b2(3): b(23) = b2(3): b(24) = b2(6):
''b(25) = b2(4): b(26) = b2(5): b(27) = b2(5): b(28) = b2(4):
''b(29) = b2(4): b(30) = b2(4): b(31) = b2(5): b(32) = b2(5):
''b(33) = b2(5): b(34) = b2(4): b(35) = b2(4): b(36) = b2(5):
''b(37) = b2(5): b(38) = b2(5): b(39) = b2(4): b(40) = b2(4):
''b(41) = b2(6): b(42) = b2(3): b(43) = b2(6): b(44) = b2(3):
''b(45) = b2(3): b(46) = b2(6): b(47) = b2(6): b(48) = b2(3):
''b(49) = b2(7): b(50) = b2(7): b(51) = b2(2): b(52) = b2(2):
''b(53) = b2(7): b(54) = b2(2): b(55) = b2(7): b(56) = b2(2):
''b(57) = b2(8): b(58) = b2(1): b(59) = b2(1): b(60) = b2(8):
''b(61) = b2(8): b(62) = b2(1): b(63) = b2(1): b(64) = b2(8):
' Composed, Pan magic Sub Squares
a(1) = a2(8): a(2) = a2(4): a(3) = a2(5): a(4) = a2(1):
a(5) = a2(3): a(6) = a2(2): a(7) = a2(7): a(8) = a2(6):
a(9) = a2(1): a(10) = a2(5): a(11) = a2(4): a(12) = a2(8):
a(13) = a2(6): a(14) = a2(7): a(15) = a2(2): a(16) = a2(3):
a(17) = a2(4): a(18) = a2(8): a(19) = a2(1): a(20) = a2(5):
a(21) = a2(2): a(22) = a2(3): a(23) = a2(6): a(24) = a2(7):
a(25) = a2(5): a(26) = a2(1): a(27) = a2(8): a(28) = a2(4):
a(29) = a2(7): a(30) = a2(6): a(31) = a2(3): a(32) = a2(2):
a(33) = a2(8): a(34) = a2(4): a(35) = a2(5): a(36) = a2(1):
a(37) = a2(3): a(38) = a2(2): a(39) = a2(7): a(40) = a2(6):
a(41) = a2(1): a(42) = a2(5): a(43) = a2(4): a(44) = a2(8):
a(45) = a2(6): a(46) = a2(7): a(47) = a2(2): a(48) = a2(3):
a(49) = a2(4): a(50) = a2(8): a(51) = a2(1): a(52) = a2(5):
a(53) = a2(2): a(54) = a2(3): a(55) = a2(6): a(56) = a2(7):
a(57) = a2(5): a(58) = a2(1): a(59) = a2(8): a(60) = a2(4):
a(61) = a2(7): a(62) = a2(6): a(63) = a2(3): a(64) = a2(2):
b(1) = b2(8): b(2) = b2(1): b(3) = b2(4): b(4) = b2(5):
b(5) = b2(8): b(6) = b2(1): b(7) = b2(4): b(8) = b2(5):
b(9) = b2(4): b(10) = b2(5): b(11) = b2(8): b(12) = b2(1):
b(13) = b2(4): b(14) = b2(5): b(15) = b2(8): b(16) = b2(1):
b(17) = b2(5): b(18) = b2(4): b(19) = b2(1): b(20) = b2(8):
b(21) = b2(5): b(22) = b2(4): b(23) = b2(1): b(24) = b2(8):
b(25) = b2(1): b(26) = b2(8): b(27) = b2(5): b(28) = b2(4):
b(29) = b2(1): b(30) = b2(8): b(31) = b2(5): b(32) = b2(4):
b(33) = b2(3): b(34) = b2(6): b(35) = b2(2): b(36) = b2(7):
b(37) = b2(3): b(38) = b2(6): b(39) = b2(2): b(40) = b2(7):
b(41) = b2(2): b(42) = b2(7): b(43) = b2(3): b(44) = b2(6):
b(45) = b2(2): b(46) = b2(7): b(47) = b2(3): b(48) = b2(6):
b(49) = b2(7): b(50) = b2(2): b(51) = b2(6): b(52) = b2(3):
b(53) = b2(7): b(54) = b2(2): b(55) = b2(6): b(56) = b2(3):
b(57) = b2(6): b(58) = b2(3): b(59) = b2(7): b(60) = b2(2):
b(61) = b2(6): b(62) = b2(3): b(63) = b2(7): b(64) = b2(2):
Return
' Exclude solutions with identical numbers
800 fl1 = 1
For j10 = 1 To 64
c2 = c(j10)
For j20 = (1 + j10) To 64
If c2 = c(j20) Then fl1 = 0: Return
Next j20
Next j10
Return
' Print results (selected numbers)
640 Cells(n9, 65).Select
For i1 = 1 To 64
Cells(n9, i1).Value = c(i1)
Next i1
Cells(n9, 65).Value = n9
Return
' Print results (squares)
650 n1 = n1 + 1
If n1 = 3 Then
n1 = 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 = s1
i3 = 0
For i1 = 1 To 8
For i2 = 1 To 8
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = c(i3)
Next i2
Next i1
Return
End Sub