' Generates Prime Number Eccentric Magic Squares of order 12
' Tested with Office 365 under Windows 10
Sub Priem12b()
Dim a1(1142), a(144), a10(100), b1(43300), b(43300), c(144)
y = MsgBox("Locked", vbCritical, "Routine Priem12b")
End
Sheets("Klad1").Select
n5 = 0: n9 = 0: k1 = 1: k2 = 1
ShtNm1 = "Pairs8"
ShtNm2 = "Lines10"
t1 = Timer
For j100 = 13 To 88
GoSub 2000: If fl1 = 0 Then GoTo 1500 'Define Variables
t11 = Timer 'Time Out
For j133 = m1 To m2
If b1(a1(j133)) = 0 Then GoTo 1330
If b(a1(j133)) = 0 Then b(a1(j133)) = a1(j133): c(133) = a1(j133) Else GoTo 1330
a(133) = a1(j133)
a(134) = Pr4 - a(133): If b(a(134)) = 0 Then b(a(134)) = a(134): c(134) = a(134) Else GoTo 1340
For j122 = m1 To m2
If b1(a1(j122)) = 0 Then GoTo 1220
If b(a1(j122)) = 0 Then b(a1(j122)) = a1(j122): c(122) = a1(j122) Else GoTo 1220
a(122) = a1(j122)
a(121) = Pr4 - a(122): If b(a(121)) = 0 Then b(a(121)) = a(121): c(121) = a(121) Else GoTo 1210
For j23 = m1 To m2
If b1(a1(j23)) = 0 Then GoTo 230
If b(a1(j23)) = 0 Then b(a1(j23)) = a1(j23): c(23) = a1(j23) Else GoTo 230
a(23) = a1(j23)
a(11) = Pr4 - a(23): If b(a(11)) = 0 Then b(a(11)) = a(11): c(11) = a(11) Else GoTo 110
a(12) = s12 - a(133) - a(122) - a(111) - a(100) - a(89) - a(78) - a(67) - a(56) - a(45) - a(34) - a(23)
If a(12) < a1(m1) Or a(12) > a1(m2) Then GoTo 120:
If b1(a(12)) = 0 Then GoTo 120
If b(a(12)) = 0 Then b(a(12)) = a(12): c(12) = a(12) Else GoTo 120
a(24) = Pr4 - a(12): If b(a(24)) = 0 Then b(a(24)) = a(24): c(24) = a(24) Else GoTo 240
For j110 = m1 To m2
If b1(a1(j110)) = 0 Then GoTo 1100
If b(a1(j110)) = 0 Then b(a1(j110)) = a1(j110): c(110) = a1(j110) Else GoTo 1100
a(110) = a1(j110)
a(109) = Pr4 - a(110): If b(a(109)) = 0 Then b(a(109)) = a(109): c(109) = a(109) Else GoTo 1090
a(98) = s4 - a(110) - a(122) - a(134)
If a(98) < a1(m1) Or a(98) > a1(m2) Then GoTo 980:
If b1(a(98)) = 0 Then GoTo 980
If b(a(98)) = 0 Then b(a(98)) = a(98): c(98) = a(98) Else GoTo 980
a(97) = Pr4 - a(98): If b(a(97)) = 0 Then b(a(97)) = a(97): c(97) = a(97) Else GoTo 970
For j22 = m1 To m2
If b1(a1(j22)) = 0 Then GoTo 220
If b(a1(j22)) = 0 Then b(a1(j22)) = a1(j22): c(22) = a1(j22) Else GoTo 220
a(22) = a1(j22)
a(10) = Pr4 - a(22): If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 100
a(21) = s4 - a(22) - a(23) - a(24)
If a(21) < a1(m1) Or a(21) > a1(m2) Then GoTo 210:
If b1(a(21)) = 0 Then GoTo 210
If b(a(21)) = 0 Then b(a(21)) = a(21): c(21) = a(21) Else GoTo 210
a(9) = Pr4 - a(21): If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 90
For j1 = m1 To m2
If b1(a1(j1)) = 0 Then GoTo 10
If b(a1(j1)) = 0 Then b(a1(j1)) = a1(j1): c(1) = a1(j1) Else GoTo 10
a(1) = a1(j1)
a(14) = Pr4 - a(1): If b(a(14)) = 0 Then b(a(14)) = a(14): c(14) = a(14) Else GoTo 140
For j2 = j1 + 1 To m2
If b1(a1(j2)) = 0 Then GoTo 20
If b(a1(j2)) = 0 Then b(a1(j2)) = a1(j2): c(2) = a1(j2) Else GoTo 20
a(2) = a1(j2)
a(13) = Pr4 - a(2): If b(a(13)) = 0 Then b(a(13)) = a(13): c(13) = a(13) Else GoTo 130
For j3 = j2 + 1 To m2
If b1(a1(j3)) = 0 Then GoTo 30
If b(a1(j3)) = 0 Then b(a1(j3)) = a1(j3): c(3) = a1(j3) Else GoTo 30
a(3) = a1(j3)
a(15) = Pr4 - a(3): If b(a(15)) = 0 Then b(a(15)) = a(15): c(15) = a(15) Else GoTo 150
a(4) = s4 - a(3) - a(2) - a(1)
If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 40:
If b1(a(4)) = 0 Then GoTo 40
If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 40
a(16) = Pr4 - a(4): If b(a(16)) = 0 Then b(a(16)) = a(16): c(16) = a(16) Else GoTo 160
For j25 = j13 + 1 To m2
If b1(a1(j25)) = 0 Then GoTo 250
If b(a1(j25)) = 0 Then b(a1(j25)) = a1(j25): c(25) = a1(j25) Else GoTo 250
a(25) = a1(j25)
a(26) = Pr4 - a(25): If b(a(26)) = 0 Then b(a(26)) = a(26): c(26) = a(26) Else GoTo 260
a(37) = s4 - a(25) - a(13) - a(1)
If a(37) < a1(m1) Or a(37) > a1(m2) Then GoTo 370:
If b1(a(37)) = 0 Then GoTo 370
If b(a(37)) = 0 Then b(a(37)) = a(37): c(37) = a(37) Else GoTo 370
a(38) = Pr4 - a(37): If b(a(38)) = 0 Then b(a(38)) = a(38): c(38) = a(38) Else GoTo 380
For j5 = m1 To m2
If b1(a1(j5)) = 0 Then GoTo 50
If b(a1(j5)) = 0 Then b(a1(j5)) = a1(j5): c(5) = a1(j5) Else GoTo 50
a(5) = a1(j5)
a(17) = Pr4 - a(5): If b(a(17)) = 0 Then b(a(17)) = a(17): c(17) = a(17) Else GoTo 170
For j6 = j5 + 1 To m2
If b1(a1(j6)) = 0 Then GoTo 60
If b(a1(j6)) = 0 Then b(a1(j6)) = a1(j6): c(6) = a1(j6) Else GoTo 60
a(6) = a1(j6)
a(18) = Pr4 - a(6): If b(a(18)) = 0 Then b(a(18)) = a(18): c(18) = a(18) Else GoTo 180
For j7 = j6 + 1 To m2
If b1(a1(j7)) = 0 Then GoTo 70
If b(a1(j7)) = 0 Then b(a1(j7)) = a1(j7): c(7) = a1(j7) Else GoTo 70
a(7) = a1(j7)
a(19) = Pr4 - a(7): If b(a(19)) = 0 Then b(a(19)) = a(19): c(19) = a(19) Else GoTo 190
a(8) = s4 - a(7) - a(6) - a(5)
If a(8) < a1(m1) Or a(8) > a1(m2) Then GoTo 80:
If b1(a(8)) = 0 Then GoTo 80
If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 80
a(20) = Pr4 - a(8): If b(a(20)) = 0 Then b(a(20)) = a(20): c(20) = a(20) Else GoTo 200
For j49 = m1 To m2
If b1(a1(j49)) = 0 Then GoTo 490
If b(a1(j49)) = 0 Then b(a1(j49)) = a1(j49): c(49) = a1(j49) Else GoTo 490
a(49) = a1(j49)
a(50) = Pr4 - a(49): If b(a(50)) = 0 Then b(a(50)) = a(50): c(50) = a(50) Else GoTo 500
For j61 = j49 + 1 To m2
If b1(a1(j61)) = 0 Then GoTo 610
If b(a1(j61)) = 0 Then b(a1(j61)) = a1(j61): c(61) = a1(j61) Else GoTo 610
a(61) = a1(j61)
a(62) = Pr4 - a(61): If b(a(62)) = 0 Then b(a(62)) = a(62): c(62) = a(62) Else GoTo 620
For j73 = j61 + 1 To m2
If b1(a1(j73)) = 0 Then GoTo 730
If b(a1(j73)) = 0 Then b(a1(j73)) = a1(j73): c(73) = a1(j73) Else GoTo 730
a(73) = a1(j73)
a(74) = Pr4 - a(73): If b(a(74)) = 0 Then b(a(74)) = a(74): c(74) = a(74) Else GoTo 740
a(85) = s4 - a(73) - a(61) - a(49)
If a(85) < a1(m1) Or a(85) > a1(m2) Then GoTo 850:
If b1(a(85)) = 0 Then GoTo 850
If b(a(85)) = 0 Then b(a(85)) = a(85): c(85) = a(85) Else GoTo 850
a(86) = Pr4 - a(85): If b(a(86)) = 0 Then b(a(86)) = a(86): c(86) = a(86) Else GoTo 860
' Exclude solutions with identical numbers (Back Check)
GoSub 800: If fl1 = 0 Then GoTo 5
n9 = n9 + 1: GoSub 1650 'Print results (squares)
' n9 = n9 + 1: GoSub 1640 'Print results (selected numbers)
Erase b, c: GoTo 1500 'Print only first square
5
b(c(86)) = 0: c(86) = 0
860 b(c(85)) = 0: c(85) = 0
850 b(c(74)) = 0: c(74) = 0
740 b(c(73)) = 0: c(73) = 0
730 Next j73
b(c(62)) = 0: c(62) = 0
620 b(c(61)) = 0: c(61) = 0
610 Next j61
b(c(50)) = 0: c(50) = 0
500 b(c(49)) = 0: c(49) = 0
490 Next j49
b(c(20)) = 0: c(20) = 0
200 b(c(8)) = 0: c(8) = 0
80 b(c(19)) = 0: c(19) = 0
190 b(c(7)) = 0: c(7) = 0
70 Next j7
b(c(18)) = 0: c(18) = 0
180 b(c(6)) = 0: c(6) = 0
60 Next j6
b(c(17)) = 0: c(17) = 0
170 b(c(5)) = 0: c(5) = 0
50 Next j5
b(c(38)) = 0: c(38) = 0
380 b(c(37)) = 0: c(37) = 0
370 b(c(26)) = 0: c(26) = 0
260 b(c(25)) = 0: c(25) = 0
250 Next j25
b(c(16)) = 0: c(16) = 0
160 b(c(4)) = 0: c(4) = 0
40 b(c(15)) = 0: c(15) = 0
150 b(c(3)) = 0: c(3) = 0
30 Next j3
b(c(13)) = 0: c(13) = 0
130 b(c(2)) = 0: c(2) = 0
20 Next j2
b(c(14)) = 0: c(14) = 0
140 b(c(1)) = 0: c(1) = 0
10 Next j1
b(c(9)) = 0: c(9) = 0
90 b(c(21)) = 0: c(21) = 0
210 b(c(10)) = 0: c(10) = 0
100 b(c(22)) = 0: c(22) = 0
220 Next j22
b(c(97)) = 0: c(97) = 0
970 b(c(98)) = 0: c(98) = 0
980 b(c(109)) = 0: c(109) = 0
1090 b(c(110)) = 0: c(110) = 0
1100 Next j110
b(c(24)) = 0: c(24) = 0
240 b(c(12)) = 0: c(12) = 0
120 b(c(11)) = 0: c(11) = 0
110 b(c(23)) = 0: c(23) = 0
230 Next j23
b(c(121)) = 0: c(121) = 0
1210 b(c(122)) = 0: c(122) = 0
1220 Next j122
b(c(134)) = 0: c(134) = 0
1340 b(c(133)) = 0: c(133) = 0
1330 Next j133
1500 Erase b1, b, c
Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Priem12b")
End
' Double Check Identical Numbers a()
800 fl1 = 1
For i1 = 1 To 144
a20 = a(i1): If a20 = 0 Then GoTo 810
For i2 = (1 + i1) To 144
If a20 = a(i2) Then fl1 = 0: Return
Next i2
810 Next i1
Return
' Remove used pairs from b1()
950 For i1 = 1 To n32
b1(a10(i1)) = 0
b1(Pr4 - a10(i1)) = 0 'Complement
Next i1
Return
' Print results (lines)
1640 ''Cells(n9, 144).Select
For i1 = 1 To 144
Cells(n9, i1).Value = a(i1)
Next i1
Return
' Print results (squares)
1650 n2 = n2 + 1
If n2 = 3 Then
n2 = 1: k1 = k1 + 13: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 13
End If
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = CStr(s12)
i3 = 0
For i1 = 1 To 12
For i2 = 1 To 12
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a(i3)
Next i2
Next i1
Return
' Defien Variables
2000 fl1 = 1
' Start Reading Data ShtNm2
Rcrd1a = Sheets(ShtNm2).Cells(j100, 102).Value
MC10 = Sheets(ShtNm2).Cells(j100, 101).Value
' Read Prime Numbers From Sheet ShtNm1
Pr4 = Sheets(ShtNm1).Cells(Rcrd1a, 1).Value 'PairSum
s4 = 2 * Pr4 'MC4
s10 = 5 * Pr4 'MC10
s12 = 6 * Pr4 'MC12
nVar = Sheets(ShtNm1).Cells(Rcrd1a, 5).Value
If nVar <= 146 Then fl1 = 0: Return
If MC10 <> s10 Then
y = MsgBox("Conflict in Data", vbCritical, "Read " + ShtNm2)
End
End If
Erase b1
For j1 = 1 To nVar
x = Sheets(ShtNm1).Cells(Rcrd1a, 6 + j1).Value
b1(x) = x
Next j1
pMax = Sheets(ShtNm1).Cells(Rcrd1a, 6 + nVar).Value
' Read Eccentric Square 10 x 10
For i1 = 1 To 100
a10(i1) = Sheets(ShtNm2).Cells(j100, i1).Value
Next i1
n32 = 100: GoSub 950 'Remove used primes from available primes
Erase a
a(27) = a10(1): a(28) = a10(2): a(29) = a10(3): a(30) = a10(4): a(31) = a10(5):
a(32) = a10(6): a(33) = a10(7): a(34) = a10(8): a(35) = a10(9): a(36) = a10(10):
a(39) = a10(11): a(40) = a10(12): a(41) = a10(13): a(42) = a10(14): a(43) = a10(15):
a(44) = a10(16): a(45) = a10(17): a(46) = a10(18): a(47) = a10(19): a(48) = a10(20):
a(51) = a10(21): a(52) = a10(22): a(53) = a10(23): a(54) = a10(24): a(55) = a10(25):
a(56) = a10(26): a(57) = a10(27): a(58) = a10(28): a(59) = a10(29): a(60) = a10(30):
a(63) = a10(31): a(64) = a10(32): a(65) = a10(33): a(66) = a10(34): a(67) = a10(35):
a(68) = a10(36): a(69) = a10(37): a(70) = a10(38): a(71) = a10(39): a(72) = a10(40):
a(75) = a10(41): a(76) = a10(42): a(77) = a10(43): a(78) = a10(44): a(79) = a10(45):
a(80) = a10(46): a(81) = a10(47): a(82) = a10(48): a(83) = a10(49): a(84) = a10(50):
a(87) = a10(51): a(88) = a10(52): a(89) = a10(53): a(90) = a10(54): a(91) = a10(55):
a(92) = a10(56): a(93) = a10(57): a(94) = a10(58): a(95) = a10(59): a(96) = a10(60):
a(99) = a10(61): a(100) = a10(62): a(101) = a10(63): a(102) = a10(64): a(103) = a10(65):
a(104) = a10(66): a(105) = a10(67): a(106) = a10(68): a(107) = a10(69): a(108) = a10(70):
a(111) = a10(71): a(112) = a10(72): a(113) = a10(73): a(114) = a10(74): a(115) = a10(75):
a(116) = a10(76): a(117) = a10(77): a(118) = a10(78): a(119) = a10(79): a(120) = a10(80):
a(123) = a10(81): a(124) = a10(82): a(125) = a10(83): a(126) = a10(84): a(127) = a10(85):
a(128) = a10(86): a(129) = a10(87): a(130) = a10(88): a(131) = a10(89): a(132) = a10(90):
a(135) = a10(91): a(136) = a10(92): a(137) = a10(93): a(138) = a10(94): a(139) = a10(95):
a(140) = a10(96): a(141) = a10(97): a(142) = a10(98): a(143) = a10(99): a(144) = a10(100):
Erase a10
' 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
Return
End Sub