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