' Generates Prime Number Concentric Magic Squares of order 13
' Diamond Inlays order 7
' Tested with Office 365 under Windows 10
Sub MgcSqr13a()
Dim a1(2448), a(169), b(43300), b1(43300), c(169)
y = MsgBox("Locked", vbCritical, "Routine MgcSqr13a")
End
n5 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
Sheets("Klad1").Select
t1 = Timer
For j101 = 12 To 14
' Read Order 11 Conc Square / Diamond Inlay
Erase b
For i1 = 1 To 169
a(i1) = Sheets("DiaLns11").Cells(j101, i1):
If a(i1) <> 0 Then b(a(i1)) = a(i1)
Next i1
j100 = Sheets("DiaLns7").Cells(j101, 171):
' Define variables
p2 = Sheets("Pairs7").Cells(j100, 1).Value 'Pair Sum
s1 = 13 * p2 / 2
nVar1 = Sheets("Pairs7").Cells(j100, 9).Value
For i1 = 1 To nVar1
a1(i1) = Sheets("Pairs7").Cells(j100, 9 + i1).Value
Next i1
m1 = 1: m2 = nVar1
If a1(1) = 1 Then m1 = 2: m2 = m2 - 1
Erase b1
For i1 = m1 To m2
b1(a1(i1)) = a1(i1)
Next i1
' Complete Border
n10 = 0
For j169 = m2 To m1 Step -1 'a(169)
If b(a1(j169)) = 0 Then b(a1(j169)) = a1(j169): c(169) = a1(j169) Else GoTo 1690
a(169) = a1(j169)
a(1) = p2 - a(169): If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 10
For j168 = j169 - 1 To m1 Step -1 'a(168)
If b(a1(j168)) = 0 Then b(a1(j168)) = a1(j168): c(168) = a1(j168) Else GoTo 1680
a(168) = a1(j168)
a(12) = p2 - a(168): If b(a(12)) = 0 Then b(a(12)) = a(12): c(12) = a(12) Else GoTo 120
For j167 = j168 - 1 To m1 Step -1 'a(167)
If b(a1(j167)) = 0 Then b(a1(j167)) = a1(j167): c(167) = a1(j167) Else GoTo 1670
a(167) = a1(j167)
a(11) = p2 - a(167): If b(a(11)) = 0 Then b(a(11)) = a(11): c(11) = a(11) Else GoTo 110
For j166 = j167 - 1 To m1 Step -1 'a(166)
If b(a1(j166)) = 0 Then b(a1(j166)) = a1(j166): c(166) = a1(j166) Else GoTo 1660
a(166) = a1(j166)
a(10) = p2 - a(166): If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 100
For j165 = j166 - 1 To m1 Step -1 'a(165)
If b(a1(j165)) = 0 Then b(a1(j165)) = a1(j165): c(165) = a1(j165) Else GoTo 1650
a(165) = a1(j165)
a(9) = p2 - a(165): If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 90
For j164 = j165 - 1 To m1 Step -1 'a(164)
If b(a1(j164)) = 0 Then b(a1(j164)) = a1(j164): c(164) = a1(j164) Else GoTo 1640
a(164) = a1(j164)
a(8) = p2 - a(164): If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 80
For j157 = m1 To m2 'a(157)
If b(a1(j157)) = 0 Then b(a1(j157)) = a1(j157): c(157) = a1(j157) Else GoTo 1570
a(157) = a1(j157)
a(13) = p2 - a(157): If b(a(13)) = 0 Then b(a(13)) = a(13): c(13) = a(13) Else GoTo 130
For j162 = j157 + 1 To m2 'a(162)
If b(a1(j162)) = 0 Then b(a1(j162)) = a1(j162): c(162) = a1(j162) Else GoTo 1620
a(162) = a1(j162)
a(6) = p2 - a(162): If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 60
For j161 = j162 + 1 To m2 'a(161)
If b(a1(j161)) = 0 Then b(a1(j161)) = a1(j161): c(161) = a1(j161) Else GoTo 1610
a(161) = a1(j161)
a(5) = p2 - a(161): If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 50
For j160 = j161 + 1 To m2 'a(160)
If b(a1(j160)) = 0 Then b(a1(j160)) = a1(j160): c(160) = a1(j160) Else GoTo 1600
a(160) = a1(j160)
a(4) = p2 - a(160): If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 40
For j159 = j160 + 1 To m2 'a(159)
If b(a1(j159)) = 0 Then b(a1(j159)) = a1(j159): c(159) = a1(j159) Else GoTo 1590
a(159) = a1(j159)
a(3) = p2 - a(159): If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 30
a(158) = s1 - a(157) - a(159) - a(160) - a(161) - a(162) - a(163) - a(164) - a(165) - a(166) - a(167) - a(168) - a(169)
If a(158) < a1(m1) Or a(158) > a1(m2) Then GoTo 1580
If b1(a(158)) = 0 Then GoTo 1580
If b(a(158)) = 0 Then b(a(158)) = a(158): c(158) = a(158) Else GoTo 1580
a(2) = p2 - a(158): If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 20
For j156 = m2 To m1 Step -1 'a(156)
If b(a1(j156)) = 0 Then b(a1(j156)) = a1(j156): c(156) = a1(j156) Else GoTo 1560
a(156) = a1(j156)
a(144) = p2 - a(156): If b(a(144)) = 0 Then b(a(144)) = a(144): c(144) = a(144) Else GoTo 1440
For j143 = j156 - 1 To m1 Step -1 'a(143)
If b(a1(j143)) = 0 Then b(a1(j143)) = a1(j143): c(143) = a1(j143) Else GoTo 1430
a(143) = a1(j143)
a(131) = p2 - a(143): If b(a(131)) = 0 Then b(a(131)) = a(131): c(131) = a(131) Else GoTo 1310
For j130 = j143 - 1 To m1 Step -1 'a(130)
If b(a1(j130)) = 0 Then b(a1(j130)) = a1(j130): c(130) = a1(j130) Else GoTo 1300
a(130) = a1(j130)
a(118) = p2 - a(130): If b(a(118)) = 0 Then b(a(118)) = a(118): c(118) = a(118) Else GoTo 1180
For j117 = j130 - 1 To m1 Step -1 'a(117)
If b(a1(j117)) = 0 Then b(a1(j117)) = a1(j117): c(117) = a1(j117) Else GoTo 1170
a(117) = a1(j117)
a(105) = p2 - a(117): If b(a(105)) = 0 Then b(a(105)) = a(105): c(105) = a(105) Else GoTo 1050
For j104 = m1 To m2 'a(104)
If b(a1(j104)) = 0 Then b(a1(j104)) = a1(j104): c(104) = a1(j104) Else GoTo 1040
a(104) = a1(j104)
a(92) = p2 - a(104): If b(a(92)) = 0 Then b(a(92)) = a(92): c(92) = a(92) Else GoTo 920
For j78 = m1 To m2 'a(78)
If b(a1(j78)) = 0 Then b(a1(j78)) = a1(j78): c(78) = a1(j78) Else GoTo 780
a(78) = a1(j78)
a(66) = p2 - a(78): If b(a(66)) = 0 Then b(a(66)) = a(66): c(66) = a(66) Else GoTo 660
For j65 = m1 To m2 'a(65)
If b(a1(j65)) = 0 Then b(a1(j65)) = a1(j65): c(65) = a1(j65) Else GoTo 650
a(65) = a1(j65)
a(53) = p2 - a(65): If b(a(53)) = 0 Then b(a(53)) = a(53): c(53) = a(53) Else GoTo 530
For j52 = m1 To m2 'a(52)
If b(a1(j52)) = 0 Then b(a1(j52)) = a1(j52): c(52) = a1(j52) Else GoTo 520
a(52) = a1(j52)
a(40) = p2 - a(52): If b(a(40)) = 0 Then b(a(40)) = a(40): c(40) = a(40) Else GoTo 400
For j39 = m1 To m2 'a(39)
If b(a1(j39)) = 0 Then b(a1(j39)) = a1(j39): c(39) = a1(j39) Else GoTo 390
a(39) = a1(j39)
a(27) = p2 - a(39): If b(a(27)) = 0 Then b(a(27)) = a(27): c(27) = a(27) Else GoTo 270
a(26) = s1 - a(13) - a(39) - a(52) - a(65) - a(78) - a(91) - a(104) - a(117) - a(130) - a(143) - a(156) - a(169)
If a(26) < a1(m1) Or a(26) > a1(m2) Then GoTo 260
If b1(a(26)) = 0 Then GoTo 260
If b(a(26)) = 0 Then b(a(26)) = a(26): c(26) = a(26) Else GoTo 260
a(14) = p2 - a(26): If b(a(14)) = 0 Then b(a(14)) = a(14): c(14) = a(14) Else GoTo 140
' Exclude solutions with identical numbers
GoSub 1800: If fl1 = 0 Then GoTo 5
n9 = n9 + 1
GoSub 2650 'Print Result (Squares)
Erase b, c: GoTo 2000 'Print only first square
5
b(c(14)) = 0: c(14) = 0
140 b(c(26)) = 0: c(26) = 0
260 b(c(27)) = 0: c(27) = 0
270 b(c(39)) = 0: c(39) = 0
390 Next j39
b(c(40)) = 0: c(40) = 0
400 b(c(52)) = 0: c(52) = 0
520 Next j52
b(c(53)) = 0: c(53) = 0
530 b(c(65)) = 0: c(65) = 0
650 Next j65
b(c(66)) = 0: c(66) = 0
660 b(c(78)) = 0: c(78) = 0
780 Next j78
b(c(92)) = 0: c(92) = 0
920 b(c(104)) = 0: c(104) = 0
1040 Next j104
b(c(105)) = 0: c(105) = 0
1050 b(c(117)) = 0: c(117) = 0
1170 Next j117
b(c(118)) = 0: c(118) = 0
1180 b(c(130)) = 0: c(130) = 0
1300 Next j130
b(c(131)) = 0: c(131) = 0
1310 b(c(143)) = 0: c(143) = 0
1430 Next j143
b(c(144)) = 0: c(144) = 0
1440 b(c(156)) = 0: c(156) = 0
1560 Next j156
b(c(2)) = 0: c(2) = 0
20 b(c(158)) = 0: c(158) = 0
1580 b(c(3)) = 0: c(3) = 0
30 b(c(159)) = 0: c(159) = 0
1590 Next j159
b(c(4)) = 0: c(4) = 0
40 b(c(160)) = 0: c(160) = 0
1600 Next j160
b(c(5)) = 0: c(5) = 0
50 b(c(161)) = 0: c(161) = 0
1610 Next j161
b(c(6)) = 0: c(6) = 0
60 b(c(162)) = 0: c(162) = 0
1620 Next j162
b(c(13)) = 0: c(13) = 0
130 b(c(157)) = 0: c(157) = 0
1570 Next j157
b(c(8)) = 0: c(8) = 0
80 b(c(164)) = 0: c(164) = 0
1640 Next j164
b(c(9)) = 0: c(9) = 0
90 b(c(165)) = 0: c(165) = 0
1650 Next j165
b(c(10)) = 0: c(10) = 0
100 b(c(166)) = 0: c(166) = 0
1660 Next j166
b(c(11)) = 0: c(11) = 0
110 b(c(167)) = 0: c(167) = 0
1670 Next j167
b(c(12)) = 0: c(12) = 0
120 b(c(168)) = 0: c(168) = 0
1680 Next j168
b(c(1)) = 0: c(1) = 0
10 b(c(169)) = 0: c(169) = 0
1690 Next j169
n10 = 0: Erase b, c
2000 Next j101
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s1)
y = MsgBox(t10, 0, "Routine MgcSqr13a")
End
' Print results (selected numbers)
2645 For i1 = 1 To 169
Cells(n9, i1).Value = a(i1)
Next i1
Cells(n9, 170).Value = s1
Cells(n9, 171).Value = j100
Return
' Print results (squares)
2650 n5 = n5 + 1
If n5 = 2 Then
n5 = 1: k1 = k1 + 14: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 14
End If
Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = "Mc13 = " + CStr(s1)
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
' Exclude solutions with identical numbers
1800 fl1 = 1
For j1 = 1 To 169
a2 = a(j1): If a2 = 0 Then GoTo 1810
For j2 = (1 + j1) To 169
If a2 = a(j2) Then fl1 = 0: Return
Next j2
1810 Next j1
Return
End Sub