' Generates Prime Number Concentric Magic Squares of order 12
' Tested with Office 365 under Windows 10
Sub Priem12a()
Dim a1(1142), a(144), a10(100), b1(43300), b(43300), c(144)
y = MsgBox("Locked", vbCritical, "Routine Priem12a")
End
Sheets("Klad1").Select
n5 = 0: n9 = 0: k1 = 1: k2 = 1
ShtNm1 = "Pairs8"
ShtNm2 = "Lines10"
t1 = Timer
For j100 = 250 To 275
GoSub 2000: If fl1 = 0 Then GoTo 1500 'Define Variables
t11 = Timer 'Time Out
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(144) = Pr4 - a(1): If b(a(144)) = 0 Then b(a(144)) = a(144): c(144) = a(144) Else GoTo 1440
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)
t12 = Timer: t13 = t12 - t11 'Time Out
If t13 > 60 Then Erase b1, b, c: t11 = Timer: GoTo 1500 'Time Out, Try Next j100
a(134) = Pr4 - a(2): If b(a(134)) = 0 Then b(a(134)) = a(134): c(134) = a(134) Else GoTo 1340
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(135) = Pr4 - a(3): If b(a(135)) = 0 Then b(a(135)) = a(135): c(135) = a(135) Else GoTo 1350
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(136) = Pr4 - a(4): If b(a(136)) = 0 Then b(a(136)) = a(136): c(136) = a(136) Else GoTo 1360
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(137) = Pr4 - a(5): If b(a(137)) = 0 Then b(a(137)) = a(137): c(137) = a(137) Else GoTo 1370
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(138) = Pr4 - a(6): If b(a(138)) = 0 Then b(a(138)) = a(138): c(138) = a(138) Else GoTo 1380
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(139) = Pr4 - a(7): If b(a(139)) = 0 Then b(a(139)) = a(139): c(139) = a(139) Else GoTo 1390
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(140) = Pr4 - a(8): If b(a(140)) = 0 Then b(a(140)) = a(140): c(140) = a(140) Else GoTo 1400
For j9 = m1 To m2
If b1(a1(j9)) = 0 Then GoTo 90
If b(a1(j9)) = 0 Then b(a1(j9)) = a1(j9): c(9) = a1(j9) Else GoTo 90
a(9) = a1(j9)
a(141) = Pr4 - a(9): If b(a(141)) = 0 Then b(a(141)) = a(141): c(141) = a(141) Else GoTo 1410
For j10 = j9 + 1 To m2
If b1(a1(j10)) = 0 Then GoTo 100
If b(a1(j10)) = 0 Then b(a1(j10)) = a1(j10): c(10) = a1(j10) Else GoTo 100
a(10) = a1(j10)
a(142) = Pr4 - a(10): If b(a(142)) = 0 Then b(a(142)) = a(142): c(142) = a(142) Else GoTo 1420
For j11 = j10 + 1 To m2
If b1(a1(j11)) = 0 Then GoTo 110
If b(a1(j11)) = 0 Then b(a1(j11)) = a1(j11): c(11) = a1(j11) Else GoTo 110
a(11) = a1(j11)
a(143) = Pr4 - a(11): If b(a(143)) = 0 Then b(a(143)) = a(143): c(143) = a(143) Else GoTo 1430
a(12) = s4 - a(11) - a(10) - a(9)
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(133) = Pr4 - a(12): If b(a(133)) = 0 Then b(a(133)) = a(133): c(133) = a(133) Else GoTo 1330
For j13 = j1 + 1 To m2
If b1(a1(j13)) = 0 Then GoTo 130
If b(a1(j13)) = 0 Then b(a1(j13)) = a1(j13): c(13) = a1(j13) Else GoTo 130
a(13) = a1(j13)
a(24) = Pr4 - a(13): If b(a(24)) = 0 Then b(a(24)) = a(24): c(24) = a(24) Else GoTo 240
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(36) = Pr4 - a(25): If b(a(36)) = 0 Then b(a(36)) = a(36): c(36) = a(36) Else GoTo 360
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(48) = Pr4 - a(37): If b(a(48)) = 0 Then b(a(48)) = a(48): c(48) = a(48) Else GoTo 480
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(60) = Pr4 - a(49): If b(a(60)) = 0 Then b(a(60)) = a(60): c(60) = a(60) Else GoTo 600
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(72) = Pr4 - a(61): If b(a(72)) = 0 Then b(a(72)) = a(72): c(72) = a(72) Else GoTo 720
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(84) = Pr4 - a(73): If b(a(84)) = 0 Then b(a(84)) = a(84): c(84) = a(84) Else GoTo 840
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(96) = Pr4 - a(85): If b(a(96)) = 0 Then b(a(96)) = a(96): c(96) = a(96) Else GoTo 960
For j109 = j9 + 1 To m2
If b1(a1(j109)) = 0 Then GoTo 1090
If b(a1(j109)) = 0 Then b(a1(j109)) = a1(j109): c(109) = a1(j109) Else GoTo 1090
a(109) = a1(j109)
a(120) = Pr4 - a(109): If b(a(120)) = 0 Then b(a(120)) = a(120): c(120) = a(120) Else GoTo 1200
For j121 = j109 + 1 To m2
If b1(a1(j121)) = 0 Then GoTo 1210
If b(a1(j121)) = 0 Then b(a1(j121)) = a1(j121): c(121) = a1(j121) Else GoTo 1210
a(121) = a1(j121)
a(132) = Pr4 - a(121): If b(a(132)) = 0 Then b(a(132)) = a(132): c(132) = a(132) Else GoTo 1320
a(97) = s4 - a(121) - a(109) - a(133)
If a(97) < a1(m1) Or a(97) > a1(m2) Then GoTo 970:
If b1(a(97)) = 0 Then GoTo 970
If b(a(97)) = 0 Then b(a(97)) = a(97): c(97) = a(97) Else GoTo 970
a(108) = Pr4 - a(97): If b(a(108)) = 0 Then b(a(108)) = a(108): c(108) = a(108) Else GoTo 1080
' 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(108)) = 0: c(108) = 0
1080 b(c(97)) = 0: c(97) = 0
970 b(c(132)) = 0: c(132) = 0
1320 b(c(121)) = 0: c(121) = 0
1210 Next j121
b(c(120)) = 0: c(120) = 0
1200 b(c(109)) = 0: c(109) = 0
1090 Next j109
b(c(96)) = 0: c(96) = 0
960 b(c(85)) = 0: c(85) = 0
850 b(c(84)) = 0: c(84) = 0
840 b(c(73)) = 0: c(73) = 0
730 Next j73
b(c(72)) = 0: c(72) = 0
720 b(c(61)) = 0: c(61) = 0
610 Next j61
b(c(60)) = 0: c(60) = 0
600 b(c(49)) = 0: c(49) = 0
490 Next j49
b(c(48)) = 0: c(48) = 0
480 b(c(37)) = 0: c(37) = 0
370 b(c(36)) = 0: c(36) = 0
360 b(c(25)) = 0: c(25) = 0
250 Next j25
b(c(24)) = 0: c(24) = 0
240 b(c(13)) = 0: c(13) = 0
130 Next j13
b(c(133)) = 0: c(133) = 0
1330 b(c(12)) = 0: c(12) = 0
120 b(c(143)) = 0: c(143) = 0
1430 b(c(11)) = 0: c(11) = 0
110 Next j11
b(c(142)) = 0: c(142) = 0
1420 b(c(10)) = 0: c(10) = 0
100 Next j10
b(c(141)) = 0: c(141) = 0
1410 b(c(9)) = 0: c(9) = 0
90 Next j9
b(c(140)) = 0: c(140) = 0
1400 b(c(8)) = 0: c(8) = 0
80 b(c(139)) = 0: c(139) = 0
1390 b(c(7)) = 0: c(7) = 0
70 Next j7
b(c(138)) = 0: c(138) = 0
1380 b(c(6)) = 0: c(6) = 0
60 Next j6
b(c(137)) = 0: c(137) = 0
1370 b(c(5)) = 0: c(5) = 0
50 Next j5
b(c(136)) = 0: c(136) = 0
1360 b(c(4)) = 0: c(4) = 0
40 b(c(135)) = 0: c(135) = 0
1350 b(c(3)) = 0: c(3) = 0
30 Next j3
b(c(134)) = 0: c(134) = 0
1340 b(c(2)) = 0: c(2) = 0
20 Next j2
b(c(144)) = 0: c(144) = 0
1440 b(c(1)) = 0: c(1) = 0
10 Next j1
1500 Erase b1, b, c
Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Priem12a")
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 Concentric 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(14) = a10(1): a(15) = a10(2): a(16) = a10(3): a(17) = a10(4): a(18) = a10(5):
a(19) = a10(6): a(20) = a10(7): a(21) = a10(8): a(22) = a10(9): a(23) = a10(10):
a(26) = a10(11): a(27) = a10(12): a(28) = a10(13): a(29) = a10(14): a(30) = a10(15):
a(31) = a10(16): a(32) = a10(17): a(33) = a10(18): a(34) = a10(19): a(35) = a10(20):
a(38) = a10(21): a(39) = a10(22): a(40) = a10(23): a(41) = a10(24): a(42) = a10(25):
a(43) = a10(26): a(44) = a10(27): a(45) = a10(28): a(46) = a10(29): a(47) = a10(30):
a(50) = a10(31): a(51) = a10(32): a(52) = a10(33): a(53) = a10(34): a(54) = a10(35):
a(55) = a10(36): a(56) = a10(37): a(57) = a10(38): a(58) = a10(39): a(59) = a10(40):
a(62) = a10(41): a(63) = a10(42): a(64) = a10(43): a(65) = a10(44): a(66) = a10(45):
a(67) = a10(46): a(68) = a10(47): a(69) = a10(48): a(70) = a10(49): a(71) = a10(50):
a(74) = a10(51): a(75) = a10(52): a(76) = a10(53): a(77) = a10(54): a(78) = a10(55):
a(79) = a10(56): a(80) = a10(57): a(81) = a10(58): a(82) = a10(59): a(83) = a10(60):
a(86) = a10(61): a(87) = a10(62): a(88) = a10(63): a(89) = a10(64): a(90) = a10(65):
a(91) = a10(66): a(92) = a10(67): a(93) = a10(68): a(94) = a10(69): a(95) = a10(70):
a(98) = a10(71): a(99) = a10(72): a(100) = a10(73): a(101) = a10(74): a(102) = a10(75):
a(103) = a10(76): a(104) = a10(77): a(105) = a10(78): a(106) = a10(79): a(107) = a10(80):
a(110) = a10(81): a(111) = a10(82): a(112) = a10(83): a(113) = a10(84): a(114) = a10(85):
a(115) = a10(86): a(116) = a10(87): a(117) = a10(88): a(118) = a10(89): a(119) = a10(90):
a(122) = a10(91): a(123) = a10(92): a(124) = a10(93): a(125) = a10(94): a(126) = a10(95):
a(127) = a10(96): a(128) = a10(97): a(129) = a10(98): a(130) = a10(99): a(131) = 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