' Generates Prime Number Concentric Magic Squares of order 13
' Tested with Office 365 under Windows 10
Sub Priem13a()
Dim a1(1142), a(169), a11(121), b1(43300), b(43300), c(169)
y = MsgBox("Locked", vbCritical, "Routine Priem13a")
End
Sheets("Klad1").Select
n5 = 0: n9 = 0: k1 = 1: k2 = 1
ShtNm1 = "Pairs7"
ShtNm2 = "Lines11"
t1 = Timer
For j100 = 18 To 26
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(169) = Pr4 - a(1): If b(a(169)) = 0 Then b(a(169)) = a(169): c(169) = a(169) Else GoTo 1690
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(158) = Pr4 - a(2): If b(a(158)) = 0 Then b(a(158)) = a(158): c(158) = a(158) Else GoTo 1580
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(159) = Pr4 - a(3): If b(a(159)) = 0 Then b(a(159)) = a(159): c(159) = a(159) Else GoTo 1590
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(160) = Pr4 - a(4): If b(a(160)) = 0 Then b(a(160)) = a(160): c(160) = a(160) Else GoTo 1600
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(161) = Pr4 - a(5): If b(a(161)) = 0 Then b(a(161)) = a(161): c(161) = a(161) Else GoTo 1610
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(162) = Pr4 - a(6): If b(a(162)) = 0 Then b(a(162)) = a(162): c(162) = a(162) Else GoTo 1620
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(163) = Pr4 - a(7): If b(a(163)) = 0 Then b(a(163)) = a(163): c(163) = a(163) Else GoTo 1630
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(164) = Pr4 - a(8): If b(a(164)) = 0 Then b(a(164)) = a(164): c(164) = a(164) Else GoTo 1640
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(165) = Pr4 - a(9): If b(a(165)) = 0 Then b(a(165)) = a(165): c(165) = a(165) Else GoTo 1650
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(166) = Pr4 - a(10): If b(a(166)) = 0 Then b(a(166)) = a(166): c(166) = a(166) Else GoTo 1660
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(167) = Pr4 - a(11): If b(a(167)) = 0 Then b(a(167)) = a(167): c(167) = a(167) Else GoTo 1670
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(168) = Pr4 - a(12): If b(a(168)) = 0 Then b(a(168)) = a(168): c(168) = a(168) Else GoTo 1680
a(13) = s4 - a(12) - a(11) - a(10)
If a(13) < a1(m1) Or a(13) > a1(m2) Then GoTo 130:
If b1(a(13)) = 0 Then GoTo 130
If b(a(13)) = 0 Then b(a(13)) = a(14): c(14) = a(13) Else GoTo 130
a(157) = Pr4 - a(13): If b(a(157)) = 0 Then b(a(157)) = a(157): c(157) = a(157) Else GoTo 1570
For j14 = m1 To m2
If b1(a1(j14)) = 0 Then GoTo 140
If b(a1(j14)) = 0 Then b(a1(j14)) = a1(j14): c(14) = a1(j14) Else GoTo 140
a(14) = a1(j14)
a(26) = Pr4 - a(14): If b(a(26)) = 0 Then b(a(26)) = a(26): c(26) = a(26) Else GoTo 260
For j27 = j14 + 1 To m2
If b1(a1(j27)) = 0 Then GoTo 270
If b(a1(j27)) = 0 Then b(a1(j27)) = a1(j27): c(27) = a1(j27) Else GoTo 270
a(27) = a1(j27)
a(39) = Pr4 - a(27): If b(a(39)) = 0 Then b(a(39)) = a(39): c(39) = a(39) Else GoTo 390
a(40) = s4 - a(27) - a(14) - a(1)
If a(40) < a1(m1) Or a(40) > a1(m2) Then GoTo 400:
If b1(a(40)) = 0 Then GoTo 400
If b(a(40)) = 0 Then b(a(40)) = a(40): c(40) = a(40) Else GoTo 400
a(52) = Pr4 - a(40): If b(a(52)) = 0 Then b(a(52)) = a(52): c(52) = a(52) Else GoTo 520
For j53 = m1 To m2
If b1(a1(j53)) = 0 Then GoTo 530
If b(a1(j53)) = 0 Then b(a1(j53)) = a1(j53): c(53) = a1(j53) Else GoTo 530
a(53) = a1(j53)
a(65) = Pr4 - a(53): If b(a(65)) = 0 Then b(a(65)) = a(65): c(65) = a(65) Else GoTo 650
For j66 = j53 + 1 To m2
If b1(a1(j66)) = 0 Then GoTo 660
If b(a1(j66)) = 0 Then b(a1(j66)) = a1(j66): c(66) = a1(j66) Else GoTo 660
a(66) = a1(j66)
a(78) = Pr4 - a(66): If b(a(78)) = 0 Then b(a(78)) = a(78): c(78) = a(78) Else GoTo 780
For j79 = j66 + 1 To m2
If b1(a1(j79)) = 0 Then GoTo 790
If b(a1(j79)) = 0 Then b(a1(j79)) = a1(j79): c(79) = a1(j79) Else GoTo 790
a(79) = a1(j79)
a(91) = Pr4 - a(79): If b(a(91)) = 0 Then b(a(91)) = a(91): c(91) = a(91) Else GoTo 780
For j92 = j53 + 1 To m2
If b1(a1(j92)) = 0 Then GoTo 920
If b(a1(j92)) = 0 Then b(a1(j92)) = a1(j92): c(92) = a1(j92) Else GoTo 920
a(92) = a1(j92)
a(104) = Pr4 - a(92): If b(a(104)) = 0 Then b(a(104)) = a(104): c(104) = a(104) Else GoTo 1040
a(105) = s5 - a(92) - a(79) - a(66) - a(53)
If a(105) < a1(m1) Or a(105) > a1(m2) Then GoTo 1050:
If b1(a(105)) = 0 Then GoTo 1050
If b(a(105)) = 0 Then b(a(105)) = a(105): c(105) = a(105) Else GoTo 1050
a(117) = Pr4 - a(105): If b(a(117)) = 0 Then b(a(117)) = a(117): c(117) = a(117) Else GoTo 1170
For j118 = m1 To m2
If b1(a1(j118)) = 0 Then GoTo 1180
If b(a1(j118)) = 0 Then b(a1(j118)) = a1(j118): c(118) = a1(j118) Else GoTo 1180
a(118) = a1(j118)
a(130) = Pr4 - a(118): If b(a(130)) = 0 Then b(a(130)) = a(130): c(130) = a(130) Else GoTo 1300
For j131 = j118 + 1 To m2
If b1(a1(j131)) = 0 Then GoTo 1310
If b(a1(j131)) = 0 Then b(a1(j131)) = a1(j131): c(131) = a1(j131) Else GoTo 1310
a(131) = a1(j131)
a(143) = Pr4 - a(131): If b(a(143)) = 0 Then b(a(143)) = a(143): c(143) = a(143) Else GoTo 1430
a(144) = s4 - a(131) - a(118) - a(157)
If a(144) < a1(m1) Or a(144) > a1(m2) Then GoTo 1440:
If b1(a(144)) = 0 Then GoTo 1440
If b(a(144)) = 0 Then b(a(144)) = a(144): c(144) = a(144) Else GoTo 1440
a(156) = Pr4 - a(144): If b(a(156)) = 0 Then b(a(156)) = a(156): c(156) = a(156) Else GoTo 1560
' Exclude solutions with identical numbers (Back Check)
GoSub 800: If fl1 = 0 Then GoTo 5
n9 = n9 + 1: GoSub 2650 'Print results (squares)
' n9 = n9 + 1: GoSub 2640 'Print results (selected numbers)
Erase b, c: GoTo 1000 'Print only first square
5
b(c(156)) = 0: c(156) = 0
1560 b(c(144)) = 0: c(144) = 0
1440 b(c(143)) = 0: c(143) = 0
1430 b(c(131)) = 0: c(131) = 0
1310 Next j131
b(c(130)) = 0: c(130) = 0
1300 b(c(118)) = 0: c(118) = 0
1180 Next j118
b(c(117)) = 0: c(117) = 0
1170 b(c(105)) = 0: c(105) = 0
1050 b(c(104)) = 0: c(104) = 0
1040 b(c(92)) = 0: c(92) = 0
920 Next j92
b(c(91)) = 0: c(91) = 0
910 b(c(79)) = 0: c(79) = 0
790 Next j79
b(c(78)) = 0: c(78) = 0
780 b(c(66)) = 0: c(66) = 0
660 Next j66
b(c(65)) = 0: c(65) = 0
650 b(c(53)) = 0: c(53) = 0
530 Next j53
b(c(52)) = 0: c(52) = 0
520 b(c(40)) = 0: c(40) = 0
400 b(c(39)) = 0: c(39) = 0
390 b(c(27)) = 0: c(27) = 0
270 Next j27
b(c(26)) = 0: c(26) = 0
260 b(c(14)) = 0: c(14) = 0
140 Next j14
b(c(157)) = 0: c(157) = 0
1570 b(c(13)) = 0: c(13) = 0
130 b(c(168)) = 0: c(168) = 0
1680 b(c(12)) = 0: c(12) = 0
120 Next j12
b(c(167)) = 0: c(167) = 0
1670 b(c(11)) = 0: c(11) = 0
110 Next j11
b(c(166)) = 0: c(166) = 0
1660 b(c(10)) = 0: c(10) = 0
100 Next j10
b(c(165)) = 0: c(165) = 0
1650 b(c(9)) = 0: c(9) = 0
90 b(c(164)) = 0: c(164) = 0
1640 b(c(8)) = 0: c(8) = 0
80 Next j8
b(c(163)) = 0: c(163) = 0
1630 b(c(7)) = 0: c(7) = 0
70 Next j7
b(c(162)) = 0: c(162) = 0
1620 b(c(6)) = 0: c(6) = 0
60 Next j6
b(c(161)) = 0: c(161) = 0
1610 b(c(5)) = 0: c(5) = 0
50 Next j5
b(c(160)) = 0: c(160) = 0
1600 b(c(4)) = 0: c(4) = 0
40 b(c(159)) = 0: c(159) = 0
1590 b(c(3)) = 0: c(3) = 0
30 Next j3
b(c(158)) = 0: c(158) = 0
1580 b(c(2)) = 0: c(2) = 0
20 Next j2
b(c(169)) = 0: c(169) = 0
1690 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 Priem13a")
End
' Double Check Identical Numbers a()
800 fl1 = 1
For i1 = 1 To 169
a20 = a(i1): If a20 = 0 Then GoTo 810
For i2 = (1 + i1) To 169
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(a11(i1)) = 0
b1(Pr4 - a11(i1)) = 0 'Complement
Next i1
Return
' Print results (lines)
2640 ''Cells(n9, 169).Select
For i1 = 1 To 169
Cells(n9, i1).Value = a(i1)
Next i1
Return
' Print results (squares)
2650 n2 = n2 + 1
If n2 = 2 Then
n2 = 1: k1 = k1 + 14: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 14
End If
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = CStr(s13)
i3 = 0
For i1 = 1 To 13
For i2 = 1 To 13
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, 124).Value
MC11 = Sheets(ShtNm2).Cells(j100, 122).Value
' Read Prime Numbers From Sheet ShtNm1
Pr4 = Sheets(ShtNm1).Cells(Rcrd1a, 1).Value 'PairSum
s4 = 2 * Pr4 'MC4
s5 = 5 * Pr4 / 2 'MC5
s11 = 11 * Pr4 / 2 'MC11
s13 = 13 * Pr4 / 2 'MC13
nVar = Sheets(ShtNm1).Cells(Rcrd1a, 9).Value
If nVar < 169 Then fl1 = 0: Return
If MC11 <> s11 Then
y = MsgBox("Conflict in Data", vbCritical, "Read " + ShtNm2)
End
End If
Erase b1
For j1 = 1 To nVar
x = Sheets(ShtNm1).Cells(Rcrd1a, 9 + j1).Value
b1(x) = x
Next j1
pMax = Sheets(ShtNm1).Cells(Rcrd1a, 9 + nVar).Value
' Read Concentric Square 11 x 11
For i1 = 1 To 121
a11(i1) = Sheets(ShtNm2).Cells(j100, i1).Value
Next i1
n32 = 121: GoSub 950 'Remove used primes from available primes
Erase a
a(15) = a11(1): a(16) = a11(2): a(17) = a11(3): a(18) = a11(4): a(19) = a11(5): a(20) = a11(6):
a(21) = a11(7): a(22) = a11(8): a(23) = a11(9): a(24) = a11(10): a(25) = a11(11):
a(28) = a11(12): a(29) = a11(13): a(30) = a11(14): a(31) = a11(15): a(32) = a11(16): a(33) = a11(17):
a(34) = a11(18): a(35) = a11(19): a(36) = a11(20): a(37) = a11(21): a(38) = a11(22):
a(41) = a11(23): a(42) = a11(24): a(43) = a11(25): a(44) = a11(26): a(45) = a11(27): a(46) = a11(28):
a(47) = a11(29): a(48) = a11(30): a(49) = a11(31): a(50) = a11(32): a(51) = a11(33):
a(54) = a11(34): a(55) = a11(35): a(56) = a11(36): a(57) = a11(37): a(58) = a11(38): a(59) = a11(39):
a(60) = a11(40): a(61) = a11(41): a(62) = a11(42): a(63) = a11(43): a(64) = a11(44):
a(67) = a11(45): a(68) = a11(46): a(69) = a11(47): a(70) = a11(48): a(71) = a11(49): a(72) = a11(50):
a(73) = a11(51): a(74) = a11(52): a(75) = a11(53): a(76) = a11(54): a(77) = a11(55):
a(80) = a11(56): a(81) = a11(57): a(82) = a11(58): a(83) = a11(59): a(84) = a11(60): a(85) = a11(61):
a(86) = a11(62): a(87) = a11(63): a(88) = a11(64): a(89) = a11(65): a(90) = a11(66):
a(93) = a11(67): a(94) = a11(68): a(95) = a11(69): a(96) = a11(70): a(97) = a11(71): a(98) = a11(72):
a(99) = a11(73): a(100) = a11(74): a(101) = a11(75): a(102) = a11(76): a(103) = a11(77):
a(106) = a11(78): a(107) = a11(79): a(108) = a11(80): a(109) = a11(81): a(110) = a11(82): a(111) = a11(83):
a(112) = a11(84): a(113) = a11(85): a(114) = a11(86): a(115) = a11(87): a(116) = a11(88):
a(119) = a11(89): a(120) = a11(90): a(121) = a11(91): a(122) = a11(92): a(123) = a11(93): a(124) = a11(94):
a(125) = a11(95): a(126) = a11(96): a(127) = a11(97): a(128) = a11(98): a(129) = a11(99):
a(132) = a11(100): a(133) = a11(101): a(134) = a11(102): a(135) = a11(103): a(136) = a11(104): a(137) = a11(105):
a(138) = a11(106): a(139) = a11(107): a(140) = a11(108): a(141) = a11(109): a(142) = a11(110):
a(145) = a11(111): a(146) = a11(112): a(147) = a11(113): a(148) = a11(114): a(149) = a11(115): a(150) = a11(116):
a(151) = a11(117): a(152) = a11(118): a(153) = a11(119): a(154) = a11(120): a(155) = a11(121):
Erase a11
' 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