' Generates Prime Number Borderd Magic Squares of order 14
' Split Border, Center Square order 10
' Tested with Office 365 under Windows 10
Sub Priem14c()
Dim a1(1142), a(196), a14(196), b1(43300), b(43300), c(196)
y = MsgBox("Locked", vbCritical, "Routine Priem14c")
End
Sheets("Klad1").Select
n5 = 0: n9 = 0: k1 = 1: k2 = 1
ShtNm1 = "Pairs8"
ShtNm2 = "ConcLns10"
t1 = Timer
For j100 = 2 To 15
GoSub 1500: If fl1 = 0 Then GoTo 1000 'Define Variables
' Determine Magic Squares and Rectangles
GoSub 2000 'Determine 4 Corner Squares
GoSub 3000 'Determine 4 Border Sections
GoSub 800 'Double Check Identical Integers a14()
If fl1 = 1 Then
n9 = n9 + 1: GoSub 650 'Print Composed Squares a14()
End If
1000 Erase b1, b, c
Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Priem14c")
End
' Determine Corner Squares 4 x 4
2000 fl1 = 1
For j1 = m1 To m2
If b1(a1(j1)) = 0 Then GoTo 2010
If b(a1(j1)) = 0 Then b(a1(j1)) = a1(j1): c(1) = a1(j1) Else GoTo 2010
a(1) = a1(j1)
a(6) = Pr4 - a(1): If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 2060
For j2 = m1 To m2
If b1(a1(j2)) = 0 Then GoTo 2020
If b(a1(j2)) = 0 Then b(a1(j2)) = a1(j2): c(2) = a1(j2) Else GoTo 2020
a(2) = a1(j2)
a(5) = Pr4 - a(2): If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 2050
For j3 = m1 To m2
If b1(a1(j3)) = 0 Then GoTo 2030
If b(a1(j3)) = 0 Then b(a1(j3)) = a1(j3): c(3) = a1(j3) Else GoTo 2030
a(3) = a1(j3)
a(7) = Pr4 - a(3): If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 2070
a(4) = s4 - a(1) - a(2) - a(3)
If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 2040:
If b1(a(4)) = 0 Then GoTo 2040
If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 2040
a(8) = Pr4 - a(4): If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 2080
For j9 = m1 To m2
If b1(a1(j9)) = 0 Then GoTo 2090
If b(a1(j9)) = 0 Then b(a1(j9)) = a1(j9): c(9) = a1(j9) Else GoTo 2090
a(9) = a1(j9)
a(10) = Pr4 - a(9): If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 2100
a(11) = s4 - a(1) - a(5) - a(9)
If a(11) < a1(m1) Or a(11) > a1(m2) Then GoTo 2110:
If b1(a(11)) = 0 Then GoTo 2110
If b(a(11)) = 0 Then b(a(11)) = a(11): c(11) = a(11) Else GoTo 2110
a(12) = Pr4 - a(11): If b(a(12)) = 0 Then b(a(12)) = a(12): c(12) = a(12) Else GoTo 2120
n10 = n10 + 1
If n10 < 4 Then
GoSub 750 'Transform and Assign Corner Squares
n32 = 12: GoSub 910 'Remove used numbers a() from available numbers b1()
Erase b, c: GoTo 2010
Else
GoSub 750 'Transform and Assign Corner Squares
n32 = 12: GoSub 910 'Remove used numbers a() from available numbers b1()
End If
If n10 = 4 Then Erase b, c: Return 'Only four squares required
b(c(12)) = 0: c(12) = 0
2120 b(c(11)) = 0: c(11) = 0
2110 b(c(10)) = 0: c(10) = 0
2100 b(c(9)) = 0: c(9) = 0
2090 Next j9
b(c(8)) = 0: c(8) = 0
2080 b(c(4)) = 0: c(4) = 0
2040 b(c(7)) = 0: c(7) = 0
2070 b(c(3)) = 0: c(3) = 0
2030 Next j3
b(c(5)) = 0: c(5) = 0
2050 b(c(2)) = 0: c(2) = 0
2020 Next j2
b(c(6)) = 0: c(6) = 0
2060 b(c(1)) = 0: c(1) = 0
2010 Next j1
fl1 = 0
Return
' Determine Border Sections 2 x 6
3000 fl1 = 1
For j1 = m1 To m2
If b1(a1(j1)) = 0 Then GoTo 3010
If b(a1(j1)) = 0 Then b(a1(j1)) = a1(j1): c(1) = a1(j1) Else GoTo 3010
a(1) = a1(j1)
a(7) = Pr4 - a(1): If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 3070
For j2 = m1 To m2
If b1(a1(j2)) = 0 Then GoTo 3020
If b(a1(j2)) = 0 Then b(a1(j2)) = a1(j2): c(2) = a1(j2) Else GoTo 3020
a(2) = a1(j2)
a(8) = Pr4 - a(2): If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 3080
For j3 = m1 To m2
If b1(a1(j3)) = 0 Then GoTo 3030
If b(a1(j3)) = 0 Then b(a1(j3)) = a1(j3): c(3) = a1(j3) Else GoTo 3030
a(3) = a1(j3)
a(9) = Pr4 - a(3): If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 3090
For j4 = m1 To m2
If b1(a1(j4)) = 0 Then GoTo 3040
If b(a1(j4)) = 0 Then b(a1(j4)) = a1(j4): c(4) = a1(j4) Else GoTo 3040
a(4) = a1(j4)
a(10) = Pr4 - a(4): If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 3100
For j5 = m1 To m2
If b1(a1(j5)) = 0 Then GoTo 3050
If b(a1(j5)) = 0 Then b(a1(j5)) = a1(j5): c(5) = a1(j5) Else GoTo 3050
a(5) = a1(j5)
a(11) = Pr4 - a(5): If b(a(11)) = 0 Then b(a(11)) = a(11): c(11) = a(11) Else GoTo 3110
a(6) = s6 - a(5) - a(4) - a(3) - a(2) - a(1)
If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 3060:
If b1(a(6)) = 0 Then GoTo 3060
If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 3060
a(12) = Pr4 - a(6): If b(a(12)) = 0 Then b(a(12)) = a(12): c(12) = a(12) Else GoTo 3120
n10 = n10 + 1
If n10 < 8 Then
GoSub 750 'Transform and Assign Rectangles
n32 = 12: GoSub 910 'Remove used numbers a() from available numbers b1()
Erase b, c: GoTo 3010
Else
GoSub 750 'Transform and Assign Rectangles
n32 = 12: GoSub 910 'Remove used numbers a() from available numbers b1()
End If
If n10 = 8 Then Erase b, c: Return 'Only four squares required
b(c(12)) = 0: c(12) = 0
3120 b(c(6)) = 0: c(6) = 0
3060 b(c(11)) = 0: c(11) = 0
3110 b(c(5)) = 0: c(5) = 0
3050 Next j5
b(c(10)) = 0: c(10) = 0
3100 b(c(4)) = 0: c(4) = 0
3040 Next j4
b(c(9)) = 0: c(9) = 0
3090 b(c(3)) = 0: c(3) = 0
3030 Next j3
b(c(8)) = 0: c(8) = 0
3080 b(c(2)) = 0: c(2) = 0
3020 Next j2
b(c(7)) = 0: c(7) = 0
3070 b(c(1)) = 0: c(1) = 0
3010 Next j1
fl1 = 0
Return
' Transform and Assign Corner Squares and Border Sections
750 Select Case n10
Case 1: 'Square 1, Left Top
a14(1) = a(1): a14(2) = a(2): a14(3) = a(3): a14(4) = a(4):
a14(15) = a(5): a14(16) = a(6): a14(17) = a(7): a14(18) = a(8):
a14(29) = a(9): a14(30) = a(10):
a14(43) = a(11): a14(44) = a(12):
Case 2: 'Square 2, Right Top
a14(11) = a(4): a14(12) = a(3): a14(13) = a(2): a14(14) = a(1):
a14(25) = a(8): a14(26) = a(7): a14(27) = a(6): a14(28) = a(5):
a14(41) = a(10): a14(42) = a(9):
a14(55) = a(12): a14(56) = a(11):
Case 3: 'Square 3, Right Bottom
a14(153) = a(12): a14(154) = a(11):
a14(167) = a(10): a14(168) = a(9):
a14(179) = a(8): a14(180) = a(7): a14(181) = a(6): a14(182) = a(5):
a14(193) = a(4): a14(194) = a(3): a14(195) = a(2): a14(196) = a(1):
Case 4: 'Square 4, Left Bottom
a14(141) = a(11): a14(142) = a(12):
a14(155) = a(9): a14(156) = a(10):
a14(169) = a(5): a14(170) = a(6): a14(171) = a(7): a14(172) = a(8):
a14(183) = a(1): a14(184) = a(2): a14(185) = a(3): a14(186) = a(4):
Case 5: 'Section 1, Top
a14(5) = a(1): a14(6) = a(2): a14(7) = a(3): a14(8) = a(4): a14(9) = a(5): a14(10) = a(6):
a14(19) = a(7): a14(20) = a(8): a14(21) = a(9): a14(22) = a(10): a14(23) = a(11): a14(24) = a(12):
Case 6: 'Section 2, Right
a14(69) = a(1): a14(70) = a(7):
a14(83) = a(2): a14(84) = a(8):
a14(97) = a(3): a14(98) = a(9):
a14(111) = a(4): a14(112) = a(10):
a14(125) = a(5): a14(126) = a(11):
a14(139) = a(6): a14(140) = a(12):
Case 7: 'Section 3, Bottom
a14(173) = a(1): a14(174) = a(2): a14(175) = a(3): a14(176) = a(4): a14(177) = a(5): a14(178) = a(6):
a14(187) = a(7): a14(188) = a(8): a14(189) = a(9): a14(190) = a(10): a14(191) = a(11): a14(192) = a(12):
Case 8: 'Section 4, Left
a14(57) = a(1): a14(58) = a(7):
a14(71) = a(2): a14(72) = a(8):
a14(85) = a(3): a14(86) = a(9):
a14(99) = a(4): a14(100) = a(10):
a14(113) = a(5): a14(114) = a(11):
a14(127) = a(6): a14(128) = a(12):
End Select
Return
' Define Variables
1500 fl1 = 1
' Start Reading Data ShtNm2
Rcrd1a = Sheets(ShtNm2).Cells(j100, 103).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
s6 = 3 * Pr4 'MC6
s10 = 5 * Pr4 'MC10
s14 = 7 * Pr4 'MC14
nVar = Sheets(ShtNm1).Cells(Rcrd1a, 5).Value
If nVar < 196 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
Erase a
For i1 = 1 To 100
a(i1) = Sheets(ShtNm2).Cells(j100, i1).Value
Next i1
n32 = 100: GoSub 910 'Remove used primes from available primes
Erase a14
a14(31) = a(1): a14(32) = a(2): a14(33) = a(3): a14(34) = a(4): a14(35) = a(5):
a14(36) = a(6): a14(37) = a(7): a14(38) = a(8): a14(39) = a(9): a14(40) = a(10):
a14(45) = a(11): a14(46) = a(12): a14(47) = a(13): a14(48) = a(14): a14(49) = a(15):
a14(50) = a(16): a14(51) = a(17): a14(52) = a(18): a14(53) = a(19): a14(54) = a(20):
a14(59) = a(21): a14(60) = a(22): a14(61) = a(23): a14(62) = a(24): a14(63) = a(25):
a14(64) = a(26): a14(65) = a(27): a14(66) = a(28): a14(67) = a(29): a14(68) = a(30):
a14(73) = a(31): a14(74) = a(32): a14(75) = a(33): a14(76) = a(34): a14(77) = a(35):
a14(78) = a(36): a14(79) = a(37): a14(80) = a(38): a14(81) = a(39): a14(82) = a(40):
a14(87) = a(41): a14(88) = a(42): a14(89) = a(43): a14(90) = a(44): a14(91) = a(45):
a14(92) = a(46): a14(93) = a(47): a14(94) = a(48): a14(95) = a(49): a14(96) = a(50):
a14(101) = a(51): a14(102) = a(52): a14(103) = a(53): a14(104) = a(54): a14(105) = a(55):
a14(106) = a(56): a14(107) = a(57): a14(108) = a(58): a14(109) = a(59): a14(110) = a(60):
a14(115) = a(61): a14(116) = a(62): a14(117) = a(63): a14(118) = a(64): a14(119) = a(65):
a14(120) = a(66): a14(121) = a(67): a14(122) = a(68): a14(123) = a(69): a14(124) = a(70):
a14(129) = a(71): a14(130) = a(72): a14(131) = a(73): a14(132) = a(74): a14(133) = a(75):
a14(134) = a(76): a14(135) = a(77): a14(136) = a(78): a14(137) = a(79): a14(138) = a(80):
a14(143) = a(81): a14(144) = a(82): a14(145) = a(83): a14(146) = a(84): a14(147) = a(85):
a14(148) = a(86): a14(149) = a(87): a14(150) = a(88): a14(151) = a(89): a14(152) = a(90):
a14(157) = a(91): a14(158) = a(92): a14(159) = a(93): a14(160) = a(94): a14(161) = a(95):
a14(162) = a(96): a14(163) = a(97): a14(164) = a(98): a14(165) = a(99): a14(166) = a(100):
Erase a
' 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
' Remove used numbers a() from available numbers b1()
910 For i1 = 1 To n32
b1(a(i1)) = 0
Next i1
Return
' Double Check Identical Numbers a14()
800 fl1 = 1
For i1 = 1 To 196
a20 = a14(i1): If a20 = 0 Then GoTo 810
For i2 = (1 + i1) To 196
If a20 = a14(i2) Then fl1 = 0: Return
Next i2
810 Next i1
Return
' Print results (squares)
650 n5 = n5 + 1
If n5 = 3 Then
n5 = 1: k1 = k1 + 15: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 15
End If
Cells(k1, k2 + 1).Select
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 = a14(i3)
Next i2
Next i1
Return
End Sub