' Generates Prime Number Concentric Magic Squares of order 14
' Tested with Office 365 under Windows 10
Sub Priem14a()
Dim a1(1142), a(196), a12(144), b1(43300), b(43300), c(196)
y = MsgBox("Locked", vbCritical, "Routine Priem14a")
End
Sheets("Klad1").Select
n5 = 0: n9 = 0: k1 = 1: k2 = 1
ShtNm1 = "Pairs8"
ShtNm2 = "Lines12"
t1 = Timer
For j100 = 209 To 222
GoSub 2000: If fl1 = 0 Then GoTo 1000 'Define Variables
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(196) = Pr4 - a(1): If b(a(196)) = 0 Then b(a(196)) = a(196): c(196) = a(196) Else GoTo 1960
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(184) = Pr4 - a(2): If b(a(184)) = 0 Then b(a(184)) = a(184): c(184) = a(184) Else GoTo 1840
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(185) = Pr4 - a(3): If b(a(185)) = 0 Then b(a(185)) = a(185): c(185) = a(185) Else GoTo 1850
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(186) = Pr4 - a(4): If b(a(186)) = 0 Then b(a(186)) = a(186): c(186) = a(186) Else GoTo 1860
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(187) = Pr4 - a(5): If b(a(187)) = 0 Then b(a(187)) = a(187): c(187) = a(187) Else GoTo 1870
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(188) = Pr4 - a(6): If b(a(188)) = 0 Then b(a(188)) = a(188): c(188) = a(188) Else GoTo 1880
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(189) = Pr4 - a(7): If b(a(189)) = 0 Then b(a(189)) = a(189): c(189) = a(189) Else GoTo 1890
For j8 = j7 + 1 To m2
If b1(a1(j8)) = 0 Then GoTo 80
If b(a1(j8)) = 0 Then b(a1(j8)) = a1(j8): c(8) = a1(j8) Else GoTo 80
a(8) = a1(j8)
a(190) = Pr4 - a(8): If b(a(190)) = 0 Then b(a(190)) = a(190): c(190) = a(190) Else GoTo 1900
a(9) = s5 - a(8) - a(7) - a(6) - a(5)
If a(9) < a1(m1) Or a(9) > a1(m2) Then GoTo 90:
If b1(a(9)) = 0 Then GoTo 90
If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 90
a(191) = Pr4 - a(9): If b(a(191)) = 0 Then b(a(191)) = a(191): c(191) = a(191) Else GoTo 1910
For j10 = m1 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(192) = Pr4 - a(10): If b(a(192)) = 0 Then b(a(192)) = a(192): c(192) = a(192) Else GoTo 1920
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(193) = Pr4 - a(11): If b(a(193)) = 0 Then b(a(193)) = a(193): c(193) = a(193) Else GoTo 1930
For j12 = j11 + 1 To m2
If b1(a1(j12)) = 0 Then GoTo 120
If b(a1(j12)) = 0 Then b(a1(j12)) = a1(j12): c(12) = a1(j12) Else GoTo 120
a(12) = a1(j12)
a(194) = Pr4 - a(12): If b(a(194)) = 0 Then b(a(194)) = a(194): c(194) = a(194) Else GoTo 1940
For j13 = j12 + 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(195) = Pr4 - a(13): If b(a(195)) = 0 Then b(a(195)) = a(195): c(195) = a(195) Else GoTo 1950
a(14) = s5 - a(13) - a(12) - a(11) - a(10)
If a(14) < a1(m1) Or a(14) > a1(m2) Then GoTo 140:
If b1(a(14)) = 0 Then GoTo 140
If b(a(14)) = 0 Then b(a(14)) = a(14): c(14) = a(14) Else GoTo 140
a(183) = Pr4 - a(14): If b(a(183)) = 0 Then b(a(183)) = a(183): c(183) = a(183) Else GoTo 1830
For j15 = m1 To m2
If b1(a1(j15)) = 0 Then GoTo 150
If b(a1(j15)) = 0 Then b(a1(j15)) = a1(j15): c(15) = a1(j15) Else GoTo 150
a(15) = a1(j15)
a(28) = Pr4 - a(15): If b(a(28)) = 0 Then b(a(28)) = a(28): c(28) = a(28) Else GoTo 280
For j29 = j15 + 1 To m2
If b1(a1(j29)) = 0 Then GoTo 290
If b(a1(j29)) = 0 Then b(a1(j29)) = a1(j29): c(29) = a1(j29) Else GoTo 290
a(29) = a1(j29)
a(42) = Pr4 - a(29): If b(a(42)) = 0 Then b(a(42)) = a(42): c(42) = a(42) Else GoTo 420
a(43) = s4 - a(29) - a(15) - a(1)
If a(43) < a1(m1) Or a(43) > a1(m2) Then GoTo 430:
If b1(a(43)) = 0 Then GoTo 430
If b(a(43)) = 0 Then b(a(43)) = a(43): c(43) = a(43) Else GoTo 430
a(56) = Pr4 - a(43): If b(a(56)) = 0 Then b(a(56)) = a(56): c(56) = a(56) Else GoTo 560
For j57 = m1 To m2
If b1(a1(j57)) = 0 Then GoTo 570
If b(a1(j57)) = 0 Then b(a1(j57)) = a1(j57): c(57) = a1(j57) Else GoTo 570
a(57) = a1(j57)
a(70) = Pr4 - a(57): If b(a(70)) = 0 Then b(a(70)) = a(70): c(70) = a(70) Else GoTo 700
For j71 = j57 + 1 To m2
If b1(a1(j71)) = 0 Then GoTo 710
If b(a1(j71)) = 0 Then b(a1(j71)) = a1(j71): c(71) = a1(j71) Else GoTo 710
a(71) = a1(j71)
a(84) = Pr4 - a(71): If b(a(84)) = 0 Then b(a(84)) = a(84): c(84) = a(84) Else GoTo 840
For j85 = j71 + 1 To m2
If b1(a1(j85)) = 0 Then GoTo 850
If b(a1(j85)) = 0 Then b(a1(j85)) = a1(j85): c(85) = a1(j85) Else GoTo 850
a(85) = a1(j85)
a(98) = Pr4 - a(85): If b(a(98)) = 0 Then b(a(98)) = a(98): c(98) = a(98) Else GoTo 840
For j99 = j57 + 1 To m2
If b1(a1(j99)) = 0 Then GoTo 990
If b(a1(j99)) = 0 Then b(a1(j99)) = a1(j99): c(99) = a1(j99) Else GoTo 990
a(99) = a1(j99)
a(112) = Pr4 - a(99): If b(a(112)) = 0 Then b(a(112)) = a(112): c(112) = a(112) Else GoTo 1120
a(113) = s5 - a(99) - a(85) - a(71) - a(57)
If a(113) < a1(m1) Or a(113) > a1(m2) Then GoTo 1130:
If b1(a(113)) = 0 Then GoTo 1130
If b(a(113)) = 0 Then b(a(113)) = a(113): c(113) = a(113) Else GoTo 1130
a(126) = Pr4 - a(113): If b(a(126)) = 0 Then b(a(126)) = a(126): c(126) = a(126) Else GoTo 1260
For j127 = m1 To m2
If b1(a1(j127)) = 0 Then GoTo 1270
If b(a1(j127)) = 0 Then b(a1(j127)) = a1(j127): c(127) = a1(j127) Else GoTo 1270
a(127) = a1(j127)
a(140) = Pr4 - a(127): If b(a(140)) = 0 Then b(a(140)) = a(140): c(140) = a(140) Else GoTo 1400
For j141 = j127 + 1 To m2
If b1(a1(j141)) = 0 Then GoTo 1410
If b(a1(j141)) = 0 Then b(a1(j141)) = a1(j141): c(141) = a1(j141) Else GoTo 1410
a(141) = a1(j141)
a(154) = Pr4 - a(141): If b(a(154)) = 0 Then b(a(154)) = a(154): c(154) = a(154) Else GoTo 1540
For j155 = j141 + 1 To m2
If b1(a1(j155)) = 0 Then GoTo 1550
If b(a1(j155)) = 0 Then b(a1(j155)) = a1(j155): c(155) = a1(j155) Else GoTo 1550
a(155) = a1(j155)
a(168) = Pr4 - a(155): If b(a(168)) = 0 Then b(a(168)) = a(168): c(168) = a(168) Else GoTo 1680
a(169) = s5 - a(155) - a(141) - a(127) - a(183)
If a(169) < a1(m1) Or a(169) > a1(m2) Then GoTo 1690:
If b1(a(169)) = 0 Then GoTo 1690
If b(a(169)) = 0 Then b(a(169)) = a(169): c(169) = a(169) Else GoTo 1690
a(182) = Pr4 - a(169): If b(a(182)) = 0 Then b(a(182)) = a(182): c(182) = a(182) Else GoTo 1820
' 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 1000 'Print only first square
5
b(c(182)) = 0: c(182) = 0
1820 b(c(169)) = 0: c(169) = 0
1690 b(c(168)) = 0: c(168) = 0
1680 b(c(155)) = 0: c(155) = 0
1550 Next j155
b(c(154)) = 0: c(154) = 0
1540 b(c(141)) = 0: c(141) = 0
1410 Next j141
b(c(140)) = 0: c(140) = 0
1400 b(c(127)) = 0: c(127) = 0
1270 Next j127
b(c(126)) = 0: c(126) = 0
1260 b(c(113)) = 0: c(113) = 0
1130 b(c(112)) = 0: c(112) = 0
1120 b(c(99)) = 0: c(99) = 0
990 Next j99
b(c(98)) = 0: c(98) = 0
980 b(c(85)) = 0: c(85) = 0
850 Next j85
b(c(84)) = 0: c(84) = 0
840 b(c(71)) = 0: c(71) = 0
710 Next j71
b(c(70)) = 0: c(70) = 0
700 b(c(57)) = 0: c(57) = 0
570 Next j57
b(c(56)) = 0: c(56) = 0
560 b(c(43)) = 0: c(43) = 0
430 b(c(42)) = 0: c(42) = 0
420 b(c(29)) = 0: c(29) = 0
290 Next j29
b(c(28)) = 0: c(28) = 0
280 b(c(15)) = 0: c(15) = 0
150 Next j15
b(c(183)) = 0: c(183) = 0
1830 b(c(14)) = 0: c(14) = 0
140 b(c(195)) = 0: c(195) = 0
1950 b(c(13)) = 0: c(13) = 0
130 Next j13
b(c(194)) = 0: c(194) = 0
1940 b(c(12)) = 0: c(12) = 0
120 Next j12
b(c(193)) = 0: c(193) = 0
1930 b(c(11)) = 0: c(11) = 0
110 Next j11
b(c(192)) = 0: c(192) = 0
1920 b(c(10)) = 0: c(10) = 0
100 Next j10
b(c(191)) = 0: c(191) = 0
1910 b(c(9)) = 0: c(9) = 0
90 b(c(190)) = 0: c(190) = 0
1900 b(c(8)) = 0: c(8) = 0
80 Next j8
b(c(189)) = 0: c(189) = 0
1890 b(c(7)) = 0: c(7) = 0
70 Next j7
b(c(188)) = 0: c(188) = 0
1880 b(c(6)) = 0: c(6) = 0
60 Next j6
b(c(187)) = 0: c(187) = 0
1870 b(c(5)) = 0: c(5) = 0
50 Next j5
b(c(186)) = 0: c(186) = 0
1860 b(c(4)) = 0: c(4) = 0
40 b(c(185)) = 0: c(185) = 0
1850 b(c(3)) = 0: c(3) = 0
30 Next j3
b(c(184)) = 0: c(184) = 0
1840 b(c(2)) = 0: c(2) = 0
20 Next j2
b(c(196)) = 0: c(196) = 0
1960 b(c(1)) = 0: c(1) = 0
10 Next j1
1000 Erase b1, b, c
Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Priem14a")
End
' Double Check Identical Numbers a()
800 fl1 = 1
For i1 = 1 To 196
a20 = a(i1): If a20 = 0 Then GoTo 810
For i2 = (1 + i1) To 196
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(a12(i1)) = 0
b1(Pr4 - a12(i1)) = 0 'Complement
Next i1
Return
' Print results (lines)
1640 ''Cells(n9, 196).Select
For i1 = 1 To 196
Cells(n9, i1).Value = a(i1)
Next i1
Return
' Print results (squares)
1650 n2 = n2 + 1
If n2 = 2 Then
n2 = 1: k1 = k1 + 15: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 15
End If
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = CStr(s14)
i3 = 0
For i1 = 1 To 14
For i2 = 1 To 14
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a(i3)
Next i2
Next i1
Return
' Define Variables
2000 fl1 = 1
' Start Reading Data ShtNm2
Rcrd1a = Sheets(ShtNm2).Cells(j100, 146).Value
MC12 = Sheets(ShtNm2).Cells(j100, 145).Value
' Read Prime Numbers From Sheet ShtNm1
Pr4 = Sheets(ShtNm1).Cells(Rcrd1a, 1).Value 'PairSum
s4 = 2 * Pr4 'MC4
s5 = 5 * Pr4 / 2 'MC5
s12 = 6 * Pr4 'MC12
s14 = 7 * Pr4 'MC14
nVar = Sheets(ShtNm1).Cells(Rcrd1a, 5).Value
If nVar < 196 Then fl1 = 0: Return
If MC12 <> s12 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 12 x 12
For i1 = 1 To 144
a12(i1) = Sheets(ShtNm2).Cells(j100, i1).Value
Next i1
n32 = 144: GoSub 950 'Remove used primes from available primes
Erase a
a(16) = a12(1): a(17) = a12(2): a(18) = a12(3): a(19) = a12(4): a(20) = a12(5): a(21) = a12(6):
a(22) = a12(7): a(23) = a12(8): a(24) = a12(9): a(25) = a12(10): a(26) = a12(11): a(27) = a12(12):
a(30) = a12(13): a(31) = a12(14): a(32) = a12(15): a(33) = a12(16): a(34) = a12(17): a(35) = a12(18):
a(36) = a12(19): a(37) = a12(20): a(38) = a12(21): a(39) = a12(22): a(40) = a12(23): a(41) = a12(24):
a(44) = a12(25): a(45) = a12(26): a(46) = a12(27): a(47) = a12(28): a(48) = a12(29): a(49) = a12(30):
a(50) = a12(31): a(51) = a12(32): a(52) = a12(33): a(53) = a12(34): a(54) = a12(35): a(55) = a12(36):
a(58) = a12(37): a(59) = a12(38): a(60) = a12(39): a(61) = a12(40): a(62) = a12(41): a(63) = a12(42):
a(64) = a12(43): a(65) = a12(44): a(66) = a12(45): a(67) = a12(46): a(68) = a12(47): a(69) = a12(48):
a(72) = a12(49): a(73) = a12(50): a(74) = a12(51): a(75) = a12(52): a(76) = a12(53): a(77) = a12(54):
a(78) = a12(55): a(79) = a12(56): a(80) = a12(57): a(81) = a12(58): a(82) = a12(59): a(83) = a12(60):
a(86) = a12(61): a(87) = a12(62): a(88) = a12(63): a(89) = a12(64): a(90) = a12(65): a(91) = a12(66):
a(92) = a12(67): a(93) = a12(68): a(94) = a12(69): a(95) = a12(70): a(96) = a12(71): a(97) = a12(72):
a(100) = a12(73): a(101) = a12(74): a(102) = a12(75): a(103) = a12(76): a(104) = a12(77): a(105) = a12(78):
a(106) = a12(79): a(107) = a12(80): a(108) = a12(81): a(109) = a12(82): a(110) = a12(83): a(111) = a12(84):
a(114) = a12(85): a(115) = a12(86): a(116) = a12(87): a(117) = a12(88): a(118) = a12(89): a(119) = a12(90):
a(120) = a12(91): a(121) = a12(92): a(122) = a12(93): a(123) = a12(94): a(124) = a12(95): a(125) = a12(96):
a(128) = a12(97): a(129) = a12(98): a(130) = a12(99): a(131) = a12(100): a(132) = a12(101): a(133) = a12(102):
a(134) = a12(103): a(135) = a12(104): a(136) = a12(105): a(137) = a12(106): a(138) = a12(107): a(139) = a12(108):
a(142) = a12(109): a(143) = a12(110): a(144) = a12(111): a(145) = a12(112): a(146) = a12(113): a(147) = a12(114):
a(148) = a12(115): a(149) = a12(116): a(150) = a12(117): a(151) = a12(118): a(152) = a12(119): a(153) = a12(120):
a(156) = a12(121): a(157) = a12(122): a(158) = a12(123): a(159) = a12(124): a(160) = a12(125): a(161) = a12(126):
a(162) = a12(127): a(163) = a12(128): a(164) = a12(129): a(165) = a12(130): a(166) = a12(131): a(167) = a12(132):
a(170) = a12(133): a(171) = a12(134): a(172) = a12(135): a(173) = a12(136): a(174) = a12(137): a(175) = a12(138):
a(176) = a12(139): a(177) = a12(140): a(178) = a12(141): a(179) = a12(142): a(180) = a12(143): a(181) = a12(144):
Erase a12
' 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