' Generates Prime Number Eccentric Magic Squares of order 14
' Tested with Office 365 under Windows 10
Sub Priem14b()
Dim a1(1142), a(196), a12(144), b1(43300), b(43300), c(196)
y = MsgBox("Locked", vbCritical, "Routine Priem14b")
End
Sheets("Klad1").Select
n5 = 0: n9 = 0: k1 = 1: k2 = 1
ShtNm1 = "Pairs8"
ShtNm2 = "Lines12"
t1 = Timer
For j100 = 166 To 166 'Test
GoSub 2000: If fl1 = 0 Then GoTo 3000 'Define Variables
For j183 = m1 To m2
If b1(a1(j183)) = 0 Then GoTo 1830
If b(a1(j183)) = 0 Then b(a1(j183)) = a1(j183): c(183) = a1(j183) Else GoTo 1830
a(183) = a1(j183)
a(184) = Pr4 - a(183): If b(a(184)) = 0 Then b(a(184)) = a(184): c(184) = a(184) Else GoTo 1840
For j170 = m1 To m2
If b1(a1(j170)) = 0 Then GoTo 1700
If b(a1(j170)) = 0 Then b(a1(j170)) = a1(j170): c(170) = a1(j170) Else GoTo 1700
a(170) = a1(j170)
a(169) = Pr4 - a(170): If b(a(169)) = 0 Then b(a(169)) = a(169): c(169) = a(169) Else GoTo 1690
For j27 = m1 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(13) = Pr4 - a(27): If b(a(13)) = 0 Then b(a(13)) = a(13): c(13) = a(13) Else GoTo 130
a(14) = s14 - a(183) - a(170) - a(157) - a(144) - a(131) - a(118) - a(105) - a(92) - a(79) - a(66) - a(53) - a(40) - a(27)
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(28) = Pr4 - a(14): If b(a(28)) = 0 Then b(a(28)) = a(28): c(28) = a(28) Else GoTo 280
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(24) = Pr4 - a(10): If b(a(24)) = 0 Then b(a(24)) = a(24): c(24) = a(24) Else GoTo 240
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(25) = Pr4 - a(11): If b(a(25)) = 0 Then b(a(25)) = a(25): c(25) = a(25) Else GoTo 250
a(12) = s5 - a(10) - a(11) - a(13) - a(14)
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(26) = Pr4 - a(12): If b(a(26)) = 0 Then b(a(26)) = a(26): c(26) = a(26) Else GoTo 260
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(128) = Pr4 - a(127): If b(a(128)) = 0 Then b(a(128)) = a(128): c(128) = a(128) Else GoTo 1280
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(142) = Pr4 - a(141): If b(a(142)) = 0 Then b(a(142)) = a(142): c(142) = a(142) Else GoTo 1420
a(155) = s5 - a(127) - a(141) - a(169) - a(183)
If a(155) < a1(m1) Or a(155) > a1(m2) Then GoTo 1550:
If b1(a(155)) = 0 Then GoTo 1550
If b(a(155)) = 0 Then b(a(155)) = a(155): c(155) = a(155) Else GoTo 1550
a(156) = Pr4 - a(155): If b(a(156)) = 0 Then b(a(156)) = a(156): c(156) = a(156) Else GoTo 1560
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(19) = Pr4 - a(5): If b(a(19)) = 0 Then b(a(19)) = a(19): c(19) = a(19) Else GoTo 22
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(20) = Pr4 - a(6): If b(a(20)) = 0 Then b(a(20)) = a(20): c(20) = a(20) Else GoTo 200
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(21) = Pr4 - a(7): If b(a(21)) = 0 Then b(a(21)) = a(21): c(21) = a(21) Else GoTo 210
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(22) = Pr4 - a(8): If b(a(22)) = 0 Then b(a(22)) = a(22): c(22) = a(22) Else GoTo 220
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(23) = Pr4 - a(9): If b(a(23)) = 0 Then b(a(23)) = a(23): c(23) = a(23) Else GoTo 230
For j57 = m1 To m2
If b1(a1(j57)) = 0 Then GoTo 558
If b(a1(j57)) = 0 Then b(a1(j57)) = a1(j57): c(57) = a1(j57) Else GoTo 558
a(57) = a1(j57)
a(58) = Pr4 - a(57): If b(a(58)) = 0 Then b(a(58)) = a(58): c(58) = a(58) Else GoTo 580
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(72) = Pr4 - a(71): If b(a(72)) = 0 Then b(a(72)) = a(72): c(72) = a(72) Else GoTo 720
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(86) = Pr4 - a(85): If b(a(86)) = 0 Then b(a(86)) = a(86): c(86) = a(86) Else GoTo 720
For j99 = j85 + 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(100) = Pr4 - a(99): If b(a(100)) = 0 Then b(a(100)) = a(100): c(100) = a(100) Else GoTo 1000
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(114) = Pr4 - a(113): If b(a(114)) = 0 Then b(a(114)) = a(114): c(114) = a(114) Else GoTo 1140
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(16) = Pr4 - a(1): If b(a(16)) = 0 Then b(a(16)) = a(16): c(16) = a(16) Else GoTo 160
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(15) = Pr4 - a(2): If b(a(15)) = 0 Then b(a(15)) = a(15): c(15) = a(15) Else GoTo 150
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(17) = Pr4 - a(3): If b(a(17)) = 0 Then b(a(17)) = a(17): c(17) = a(17) Else GoTo 170
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(18) = Pr4 - a(4): If b(a(18)) = 0 Then b(a(18)) = a(18): c(18) = a(18) Else GoTo 180
For j29 = m1 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(30) = Pr4 - a(29): If b(a(30)) = 0 Then b(a(30)) = a(30): c(30) = a(30) Else GoTo 300
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(44) = Pr4 - a(43): If b(a(44)) = 0 Then b(a(44)) = a(44): c(44) = a(44) Else GoTo 440
' 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 3000 'Print only first square
5
b(c(44)) = 0: c(44) = 0
440 b(c(43)) = 0: c(43) = 0
430 b(c(30)) = 0: c(30) = 0
300 b(c(29)) = 0: c(29) = 0
290 Next j29
b(c(18)) = 0: c(18) = 0
180 b(c(4)) = 0: c(4) = 0
40 b(c(17)) = 0: c(17) = 0
170 b(c(3)) = 0: c(3) = 0
30 Next j3
b(c(15)) = 0: c(15) = 0
150 b(c(2)) = 0: c(2) = 0
20 Next j2
b(c(16)) = 0: c(16) = 0
160 b(c(1)) = 0: c(1) = 0
10 Next j1
b(c(114)) = 0: c(114) = 0
1140 b(c(113)) = 0: c(113) = 0
1130 b(c(100)) = 0: c(100) = 0
1000 b(c(99)) = 0: c(99) = 0
990 Next j99
b(c(86)) = 0: c(86) = 0
860 b(c(85)) = 0: c(85) = 0
850 Next j85
b(c(72)) = 0: c(72) = 0
720 b(c(71)) = 0: c(71) = 0
710 Next j71
b(c(58)) = 0: c(58) = 0
580 b(c(57)) = 0: c(57) = 0
558 Next j57
b(c(23)) = 0: c(23) = 0
230 b(c(9)) = 0: c(9) = 0
90 b(c(22)) = 0: c(22) = 0
220 b(c(8)) = 0: c(8) = 0
80 Next j8
b(c(21)) = 0: c(21) = 0
210 b(c(7)) = 0: c(7) = 0
70 Next j7
b(c(20)) = 0: c(20) = 0
200 b(c(6)) = 0: c(6) = 0
60 Next j6
b(c(19)) = 0: c(19) = 0
22 b(c(5)) = 0: c(5) = 0
50 Next j5
b(c(156)) = 0: c(156) = 0
1560 b(c(155)) = 0: c(155) = 0
1550 b(c(142)) = 0: c(142) = 0
1420 b(c(141)) = 0: c(141) = 0
1410 Next j141
b(c(128)) = 0: c(128) = 0
1280 b(c(127)) = 0: c(127) = 0
1270 Next j127
b(c(26)) = 0: c(26) = 0
260 b(c(12)) = 0: c(12) = 0
120 b(c(25)) = 0: c(25) = 0
250 b(c(11)) = 0: c(11) = 0
110 Next j11
b(c(24)) = 0: c(24) = 0
240 b(c(10)) = 0: c(10) = 0
100 Next j10
b(c(28)) = 0: c(28) = 0
280 b(c(14)) = 0: c(14) = 0
140 b(c(13)) = 0: c(13) = 0
130 b(c(27)) = 0: c(27) = 0
270 Next j27
b(c(169)) = 0: c(169) = 0
1690 b(c(170)) = 0: c(170) = 0
1700 Next j170
b(c(184)) = 0: c(184) = 0
1840 b(c(183)) = 0: c(183) = 0
1830 Next j183
3000 Erase b1, b, c
Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Priem14b")
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 Eccentric 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(31) = a12(1): a(32) = a12(2): a(33) = a12(3): a(34) = a12(4): a(35) = a12(5): a(36) = a12(6):
a(37) = a12(7): a(38) = a12(8): a(39) = a12(9): a(40) = a12(10): a(41) = a12(11): a(42) = a12(12):
a(45) = a12(13): a(46) = a12(14): a(47) = a12(15): a(48) = a12(16): a(49) = a12(17): a(50) = a12(18):
a(51) = a12(19): a(52) = a12(20): a(53) = a12(21): a(54) = a12(22): a(55) = a12(23): a(56) = a12(24):
a(59) = a12(25): a(60) = a12(26): a(61) = a12(27): a(62) = a12(28): a(63) = a12(29): a(64) = a12(30):
a(65) = a12(31): a(66) = a12(32): a(67) = a12(33): a(68) = a12(34): a(69) = a12(35): a(70) = a12(36):
a(73) = a12(37): a(74) = a12(38): a(75) = a12(39): a(76) = a12(40): a(77) = a12(41): a(78) = a12(42):
a(79) = a12(43): a(80) = a12(44): a(81) = a12(45): a(82) = a12(46): a(83) = a12(47): a(84) = a12(48):
a(87) = a12(49): a(88) = a12(50): a(89) = a12(51): a(90) = a12(52): a(91) = a12(53): a(92) = a12(54):
a(93) = a12(55): a(94) = a12(56): a(95) = a12(57): a(96) = a12(58): a(97) = a12(59): a(98) = a12(60):
a(101) = a12(61): a(102) = a12(62): a(103) = a12(63): a(104) = a12(64): a(105) = a12(65): a(106) = a12(66):
a(107) = a12(67): a(108) = a12(68): a(109) = a12(69): a(110) = a12(70): a(111) = a12(71): a(112) = a12(72):
a(115) = a12(73): a(116) = a12(74): a(117) = a12(75): a(118) = a12(76): a(119) = a12(77): a(120) = a12(78):
a(121) = a12(79): a(122) = a12(80): a(123) = a12(81): a(124) = a12(82): a(125) = a12(83): a(126) = a12(84):
a(129) = a12(85): a(130) = a12(86): a(131) = a12(87): a(132) = a12(88): a(133) = a12(89): a(134) = a12(90):
a(135) = a12(91): a(136) = a12(92): a(137) = a12(93): a(138) = a12(94): a(139) = a12(95): a(140) = a12(96):
a(143) = a12(97): a(144) = a12(98): a(145) = a12(99): a(146) = a12(100): a(147) = a12(101): a(148) = a12(102):
a(149) = a12(103): a(150) = a12(104): a(151) = a12(105): a(152) = a12(106): a(153) = a12(107): a(154) = a12(108):
a(157) = a12(109): a(158) = a12(110): a(159) = a12(111): a(160) = a12(112): a(161) = a12(113): a(162) = a12(114):
a(163) = a12(115): a(164) = a12(116): a(165) = a12(117): a(166) = a12(118): a(167) = a12(119): a(168) = a12(120):
a(171) = a12(121): a(172) = a12(122): a(173) = a12(123): a(174) = a12(124): a(175) = a12(125): a(176) = a12(126):
a(177) = a12(127): a(178) = a12(128): a(179) = a12(129): a(180) = a12(130): a(181) = a12(131): a(182) = a12(132):
a(185) = a12(133): a(186) = a12(134): a(187) = a12(135): a(188) = a12(136): a(189) = a12(137): a(190) = a12(138):
a(191) = a12(139): a(192) = a12(140): a(193) = a12(141): a(194) = a12(142): a(195) = a12(143): a(196) = 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