' Constructs Prime Number Magic Squares of order 12
' Tested with Office 365 under Windows 10
Sub CnstrSqrs12b()
Dim a2(12), b2(144), a(144), b(144), c(144)
y = MsgBox("Locked", vbExclamation, "Routine CnstrSqrs12b")
End
n2 = 0: n9 = 0: k1 = 1: k2 = 1
Sheets("Klad1").Select
t1 = Timer
For j1 = 2 To 5
' Read (Balanced) Magic Lines
For j2 = 1 To 12: a2(j2) = Sheets("Att103").Cells(j1, j2).Value: Next j2
For j2 = 1 To 12: b2(j2) = Sheets("Att103").Cells(j1, j2 + 13).Value: Next j2
s1 = Sheets("Att103").Cells(j1, 29).Value
' Construct squares a() and b()
GoSub 200
' Calculate Square c()
For j2 = 1 To 144
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 CnstrSqrs12b")
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(10): a(11) = a2(11): a(12) = a2(12):
a(13) = a2(12): a(14) = a2(11): a(15) = a2(10): a(16) = a2(9): a(17) = a2(8): a(18) = a2(7):
a(19) = a2(6): a(20) = a2(5): a(21) = a2(4): a(22) = a2(3): a(23) = a2(2): a(24) = a2(1):
a(25) = a2(3): a(26) = a2(4): a(27) = a2(5): a(28) = a2(6): a(29) = a2(1): a(30) = a2(2):
a(31) = a2(11): a(32) = a2(12): a(33) = a2(7): a(34) = a2(8): a(35) = a2(9): a(36) = a2(10):
a(37) = a2(10): a(38) = a2(9): a(39) = a2(8): a(40) = a2(7): a(41) = a2(12): a(42) = a2(11):
a(43) = a2(2): a(44) = a2(1): a(45) = a2(6): a(46) = a2(5): a(47) = a2(4): a(48) = a2(3):
a(49) = a2(5): a(50) = a2(6): a(51) = a2(1): a(52) = a2(2): a(53) = a2(3): a(54) = a2(4):
a(55) = a2(9): a(56) = a2(10): a(57) = a2(11): a(58) = a2(12): a(59) = a2(7): a(60) = a2(8):
a(61) = a2(8): a(62) = a2(7): a(63) = a2(12): a(64) = a2(11): a(65) = a2(10): a(66) = a2(9):
a(67) = a2(4): a(68) = a2(3): a(69) = a2(2): a(70) = a2(1): a(71) = a2(6): a(72) = a2(5):
a(73) = a2(4): a(74) = a2(3): a(75) = a2(6): a(76) = a2(5): a(77) = a2(2): a(78) = a2(1):
a(79) = a2(12): a(80) = a2(11): a(81) = a2(8): a(82) = a2(7): a(83) = a2(10): a(84) = a2(9):
a(85) = a2(9): a(86) = a2(10): a(87) = a2(7): a(88) = a2(8): a(89) = a2(11): a(90) = a2(12):
a(91) = a2(1): a(92) = a2(2): a(93) = a2(5): a(94) = a2(6): a(95) = a2(3): a(96) = a2(4):
a(97) = a2(2): a(98) = a2(1): a(99) = a2(4): a(100) = a2(3): a(101) = a2(6): a(102) = a2(5):
a(103) = a2(8): a(104) = a2(7): a(105) = a2(10): a(106) = a2(9): a(107) = a2(12): a(108) = a2(11):
a(109) = a2(11): a(110) = a2(12): a(111) = a2(9): a(112) = a2(10): a(113) = a2(7): a(114) = a2(8):
a(115) = a2(5): a(116) = a2(6): a(117) = a2(3): a(118) = a2(4): a(119) = a2(1): a(120) = a2(2):
a(121) = a2(6): a(122) = a2(5): a(123) = a2(2): a(124) = a2(1): a(125) = a2(4): a(126) = a2(3):
a(127) = a2(10): a(128) = a2(9): a(129) = a2(12): a(130) = a2(11): a(131) = a2(8): a(132) = a2(7):
a(133) = a2(7): a(134) = a2(8): a(135) = a2(11): a(136) = a2(12): a(137) = a2(9): a(138) = a2(10):
a(139) = a2(3): a(140) = a2(4): a(141) = a2(1): a(142) = a2(2): a(143) = a2(5): a(144) = a2(6):
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(10): b(11) = b2(11): b(12) = b2(12):
b(13) = b2(3): b(14) = b2(4): b(15) = b2(5): b(16) = b2(6): b(17) = b2(1): b(18) = b2(2):
b(19) = b2(11): b(20) = b2(12): b(21) = b2(7): b(22) = b2(8): b(23) = b2(9): b(24) = b2(10):
b(25) = b2(6): b(26) = b2(5): b(27) = b2(2): b(28) = b2(1): b(29) = b2(4): b(30) = b2(3):
b(31) = b2(10): b(32) = b2(9): b(33) = b2(12): b(34) = b2(11): b(35) = b2(8): b(36) = b2(7):
b(37) = b2(11): b(38) = b2(12): b(39) = b2(9): b(40) = b2(10): b(41) = b2(7): b(42) = b2(8):
b(43) = b2(5): b(44) = b2(6): b(45) = b2(3): b(46) = b2(4): b(47) = b2(1): b(48) = b2(2):
b(49) = b2(7): b(50) = b2(8): b(51) = b2(11): b(52) = b2(12): b(53) = b2(9): b(54) = b2(10):
b(55) = b2(3): b(56) = b2(4): b(57) = b2(1): b(58) = b2(2): b(59) = b2(5): b(60) = b2(6):
b(61) = b2(12): b(62) = b2(11): b(63) = b2(10): b(64) = b2(9): b(65) = b2(8): b(66) = b2(7):
b(67) = b2(6): b(68) = b2(5): b(69) = b2(4): b(70) = b2(3): b(71) = b2(2): b(72) = b2(1):
b(73) = b2(2): b(74) = b2(1): b(75) = b2(4): b(76) = b2(3): b(77) = b2(6): b(78) = b2(5):
b(79) = b2(8): b(80) = b2(7): b(81) = b2(10): b(82) = b2(9): b(83) = b2(12): b(84) = b2(11):
b(85) = b2(4): b(86) = b2(3): b(87) = b2(6): b(88) = b2(5): b(89) = b2(2): b(90) = b2(1):
b(91) = b2(12): b(92) = b2(11): b(93) = b2(8): b(94) = b2(7): b(95) = b2(10): b(96) = b2(9):
b(97) = b2(10): b(98) = b2(9): b(99) = b2(8): b(100) = b2(7): b(101) = b2(12): b(102) = b2(11):
b(103) = b2(2): b(104) = b2(1): b(105) = b2(6): b(106) = b2(5): b(107) = b2(4): b(108) = b2(3):
b(109) = b2(5): b(110) = b2(6): b(111) = b2(1): b(112) = b2(2): b(113) = b2(3): b(114) = b2(4):
b(115) = b2(9): b(116) = b2(10): b(117) = b2(11): b(118) = b2(12): b(119) = b2(7): b(120) = b2(8):
b(121) = b2(9): b(122) = b2(10): b(123) = b2(7): b(124) = b2(8): b(125) = b2(11): b(126) = b2(12):
b(127) = b2(1): b(128) = b2(2): b(129) = b2(5): b(130) = b2(6): b(131) = b2(3): b(132) = b2(4):
b(133) = b2(8): b(134) = b2(7): b(135) = b2(12): b(136) = b2(11): b(137) = b2(10): b(138) = b2(9):
b(139) = b2(4): b(140) = b2(3): b(141) = b2(2): b(142) = b2(1): b(143) = b2(6): b(144) = b2(5):
Return
' Exclude solutions with identical numbers
800 fl1 = 1
For j10 = 1 To 144
c2 = c(j10)
For j20 = (1 + j10) To 144
If c2 = c(j20) Then fl1 = 0: Return
Next j20
Next j10
Return
' Print results (selected numbers)
640 Cells(n9, 145).Select
For i1 = 1 To 144
Cells(n9, i1).Value = c(i1)
Next i1
Cells(n9, 145).Value = n9
Return
' Print results (squares)
650 n1 = n1 + 1
If n1 = 3 Then
n1 = 1: k1 = k1 + 13: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 13
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 12
For i2 = 1 To 12
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = c(i3)
Next i2
Next i1
Return
End Sub