' Generates Prime Number Eccentric Magic Squares of order 13
' Tested with Office 365 under Windows 10
Sub Priem13b()
Dim a1(1142), a(169), a11(121), b1(43300), b(43300), c(169)
''y = MsgBox("Locked", vbCritical, "Routine Priem13b")
''End
Sheets("Klad1").Select
n5 = 0: n9 = 0: k1 = 1: k2 = 1
ShtNm1 = "Pairs7"
ShtNm2 = "Lines11"
t1 = Timer
For j100 = 11 To 11
GoSub 2000: If fl1 = 0 Then GoTo 1000 'Define Variables
For j157 = m1 To m2
If b1(a1(j157)) = 0 Then GoTo 1570
If b(a1(j157)) = 0 Then b(a1(j157)) = a1(j157): c(157) = a1(j157) Else GoTo 1570
a(157) = a1(j157)
a(158) = Pr4 - a(157): If b(a(158)) = 0 Then b(a(158)) = a(158): c(158) = a(158) Else GoTo 1580
For j145 = m1 To m2
If b1(a1(j145)) = 0 Then GoTo 1450
If b(a1(j145)) = 0 Then b(a1(j145)) = a1(j145): c(145) = a1(j145) Else GoTo 1450
a(145) = a1(j145)
a(144) = Pr4 - a(145): If b(a(144)) = 0 Then b(a(144)) = a(144): c(144) = a(144) Else GoTo 1440
For j25 = m1 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(12) = Pr4 - a(25): If b(a(12)) = 0 Then b(a(12)) = a(12): c(12) = a(12) Else GoTo 120
a(13) = s13 - a(157) - a(145) - a(133) - a(121) - a(109) - a(97) - a(85) - a(73) - a(61) - a(49) - a(37) - a(25)
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(13): c(13) = a(13) Else GoTo 130
a(26) = Pr4 - a(13): If b(a(26)) = 0 Then b(a(26)) = a(26): c(26) = a(26) Else GoTo 260
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(23) = Pr4 - a(10): If b(a(23)) = 0 Then b(a(23)) = a(23): c(23) = a(23) Else GoTo 230
a(11) = s4 - a(10) - a(12) - a(13)
If a(11) < a1(m1) Or a(11) > a1(m2) Then GoTo 110:
If b1(a(11)) = 0 Then GoTo 110
If b(a(11)) = 0 Then b(a(11)) = a(11): c(11) = a(11) Else GoTo 110
a(24) = Pr4 - a(11): If b(a(24)) = 0 Then b(a(24)) = a(24): c(24) = a(24) Else GoTo 240
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(119) = Pr4 - a(118): If b(a(119)) = 0 Then b(a(119)) = a(119): c(119) = a(119) Else GoTo 1190
a(131) = s4 - a(118) - a(144) - a(157)
If a(131) < a1(m1) Or a(131) > a1(m2) Then GoTo 1310:
If b1(a(131)) = 0 Then GoTo 1310
If b(a(131)) = 0 Then b(a(131)) = a(131): c(131) = a(131) Else GoTo 1310
a(132) = Pr4 - a(131): If b(a(132)) = 0 Then b(a(132)) = a(132): c(132) = a(132) Else GoTo 1320
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(18) = Pr4 - a(5): If b(a(18)) = 0 Then b(a(18)) = a(18): c(18) = a(18) Else GoTo 180
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(19) = Pr4 - a(6): If b(a(19)) = 0 Then b(a(19)) = a(19): c(19) = a(19) Else GoTo 190
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(20) = Pr4 - a(7): If b(a(20)) = 0 Then b(a(20)) = a(20): c(20) = a(20) Else GoTo 200
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(21) = Pr4 - a(8): If b(a(21)) = 0 Then b(a(21)) = a(21): c(21) = a(21) Else GoTo 210
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(22) = Pr4 - a(9): If b(a(22)) = 0 Then b(a(22)) = a(22): c(22) = a(22) Else GoTo 220
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(54) = Pr4 - a(53): If b(a(54)) = 0 Then b(a(54)) = a(54): c(54) = a(54) Else GoTo 540
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(67) = Pr4 - a(66): If b(a(67)) = 0 Then b(a(67)) = a(67): c(67) = a(67) Else GoTo 670
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(80) = Pr4 - a(79): If b(a(80)) = 0 Then b(a(80)) = a(80): c(80) = a(80) Else GoTo 670
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(93) = Pr4 - a(92): If b(a(93)) = 0 Then b(a(93)) = a(93): c(93) = a(93) Else GoTo 930
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(106) = Pr4 - a(105): If b(a(106)) = 0 Then b(a(106)) = a(106): c(106) = a(106) Else GoTo 1167
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(15) = Pr4 - a(1): If b(a(15)) = 0 Then b(a(15)) = a(15): c(15) = a(15) Else GoTo 150
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(14) = Pr4 - a(2): If b(a(14)) = 0 Then b(a(14)) = a(14): c(14) = a(14) Else GoTo 140
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(16) = Pr4 - a(3): If b(a(16)) = 0 Then b(a(16)) = a(16): c(16) = a(16) Else GoTo 17
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(17) = Pr4 - a(4): If b(a(17)) = 0 Then b(a(17)) = a(17): c(17) = a(17) Else GoTo 170
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(28) = Pr4 - a(27): If b(a(28)) = 0 Then b(a(28)) = a(28): c(28) = a(28) Else GoTo 280
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(41) = Pr4 - a(40): If b(a(41)) = 0 Then b(a(41)) = a(41): c(41) = a(41) Else GoTo 410
' Exclude solutions with identical numbers (Back Check)
GoSub 850: 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(41)) = 0: c(41) = 0
410 b(c(40)) = 0: c(40) = 0
400 b(c(28)) = 0: c(28) = 0
280 b(c(27)) = 0: c(27) = 0
270 Next j27
b(c(17)) = 0: c(17) = 0
170 b(c(4)) = 0: c(4) = 0
40 b(c(16)) = 0: c(16) = 0
17 b(c(3)) = 0: c(3) = 0
30 Next j3
b(c(14)) = 0: c(14) = 0
140 b(c(2)) = 0: c(2) = 0
20 Next j2
b(c(15)) = 0: c(15) = 0
150 b(c(1)) = 0: c(1) = 0
10 Next j1
b(c(106)) = 0: c(106) = 0
1167 b(c(105)) = 0: c(105) = 0
1050 b(c(93)) = 0: c(93) = 0
930 b(c(92)) = 0: c(92) = 0
920 Next j92
b(c(80)) = 0: c(80) = 0
800 b(c(79)) = 0: c(79) = 0
790 Next j79
b(c(67)) = 0: c(67) = 0
670 b(c(66)) = 0: c(66) = 0
660 Next j66
b(c(54)) = 0: c(54) = 0
540 b(c(53)) = 0: c(53) = 0
530 Next j53
b(c(22)) = 0: c(22) = 0
220 b(c(9)) = 0: c(9) = 0
90 b(c(21)) = 0: c(21) = 0
210 b(c(8)) = 0: c(8) = 0
80 Next j8
b(c(20)) = 0: c(20) = 0
200 b(c(7)) = 0: c(7) = 0
70 Next j7
b(c(19)) = 0: c(19) = 0
190 b(c(6)) = 0: c(6) = 0
60 Next j6
b(c(18)) = 0: c(18) = 0
180 b(c(5)) = 0: c(5) = 0
50 Next j5
b(c(132)) = 0: c(132) = 0
1320 b(c(131)) = 0: c(131) = 0
1310 b(c(119)) = 0: c(119) = 0
1190 b(c(118)) = 0: c(118) = 0
1180 Next j118
b(c(24)) = 0: c(24) = 0
240 b(c(11)) = 0: c(11) = 0
110 b(c(23)) = 0: c(23) = 0
230 b(c(10)) = 0: c(10) = 0
100 Next j10
b(c(26)) = 0: c(26) = 0
260 b(c(13)) = 0: c(13) = 0
130 b(c(12)) = 0: c(12) = 0
120 b(c(25)) = 0: c(25) = 0
250 Next j25
b(c(144)) = 0: c(144) = 0
1440 b(c(145)) = 0: c(145) = 0
1450 Next j145
b(c(158)) = 0: c(158) = 0
1580 b(c(157)) = 0: c(157) = 0
1570 Next j157
1000 Erase b1, b, c
Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Priem13b")
End
' Double Check Identical Numbers a()
850 fl1 = 1
For i1 = 1 To 169
a20 = a(i1): If a20 = 0 Then GoTo 860
For i2 = (1 + i1) To 169
If a20 = a(i2) Then fl1 = 0: Return
Next i2
860 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 Eccentric 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(29) = a11(1): a(30) = a11(2): a(31) = a11(3): a(32) = a11(4): a(33) = a11(5): a(34) = a11(6):
a(35) = a11(7): a(36) = a11(8): a(37) = a11(9): a(38) = a11(10): a(39) = a11(11):
a(42) = a11(12): a(43) = a11(13): a(44) = a11(14): a(45) = a11(15): a(46) = a11(16): a(47) = a11(17):
a(48) = a11(18): a(49) = a11(19): a(50) = a11(20): a(51) = a11(21): a(52) = a11(22):
a(55) = a11(23): a(56) = a11(24): a(57) = a11(25): a(58) = a11(26): a(59) = a11(27): a(60) = a11(28):
a(61) = a11(29): a(62) = a11(30): a(63) = a11(31): a(64) = a11(32): a(65) = a11(33):
a(68) = a11(34): a(69) = a11(35): a(70) = a11(36): a(71) = a11(37): a(72) = a11(38): a(73) = a11(39):
a(74) = a11(40): a(75) = a11(41): a(76) = a11(42): a(77) = a11(43): a(78) = a11(44):
a(81) = a11(45): a(82) = a11(46): a(83) = a11(47): a(84) = a11(48): a(85) = a11(49): a(86) = a11(50):
a(87) = a11(51): a(88) = a11(52): a(89) = a11(53): a(90) = a11(54): a(91) = a11(55):
a(94) = a11(56): a(95) = a11(57): a(96) = a11(58): a(97) = a11(59): a(98) = a11(60): a(99) = a11(61):
a(100) = a11(62): a(101) = a11(63): a(102) = a11(64): a(103) = a11(65): a(104) = a11(66):
a(107) = a11(67): a(108) = a11(68): a(109) = a11(69): a(110) = a11(70): a(111) = a11(71): a(112) = a11(72):
a(113) = a11(73): a(114) = a11(74): a(115) = a11(75): a(116) = a11(76): a(117) = a11(77):
a(120) = a11(78): a(121) = a11(79): a(122) = a11(80): a(123) = a11(81): a(124) = a11(82): a(125) = a11(83):
a(126) = a11(84): a(127) = a11(85): a(128) = a11(86): a(129) = a11(87): a(130) = a11(88):
a(133) = a11(89): a(134) = a11(90): a(135) = a11(91): a(136) = a11(92): a(137) = a11(93): a(138) = a11(94):
a(139) = a11(95): a(140) = a11(96): a(141) = a11(97): a(142) = a11(98): a(143) = a11(99):
a(146) = a11(100): a(147) = a11(101): a(148) = a11(102): a(149) = a11(103): a(150) = a11(104): a(151) = a11(105):
a(152) = a11(106): a(153) = a11(107): a(154) = a11(108): a(155) = a11(109): a(156) = a11(110):
a(159) = a11(111): a(160) = a11(112): a(161) = a11(113): a(162) = a11(114): a(163) = a11(115): a(164) = a11(116):
a(165) = a11(117): a(166) = a11(118): a(167) = a11(119): a(168) = a11(120): a(169) = 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