' Generates Borders for:
' Quadrant P14 Bordered Magic Squares of order 13, MC = 1105
' Tested with Office 365 under Windows 10
Sub Priem13a()
Dim a1(48), a(169), b1(169), b(169), c(169)
y = MsgBox("Locked", vbCritical, "Routine Priem13a")
End
n2 = 0: n3 = 0: k1 = 1: k2 = 1: n9 = 0: n10 = 0
s1 = 1105: Pr13 = 170
m1 = 1: m2 = 48
' Define Integers
a1(1) = 1: a1(2) = 2: a1(3) = 3: a1(4) = 4: a1(5) = 5: a1(6) = 6: a1(7) = 7: a1(8) = 8:
a1(9) = 9: a1(10) = 10: a1(11) = 11: a1(12) = 12: a1(13) = 13: a1(14) = 14: a1(15) = 26: a1(16) = 27:
a1(17) = 39: a1(18) = 40: a1(19) = 52: a1(20) = 53: a1(21) = 65: a1(22) = 66: a1(23) = 78: a1(24) = 79:
a1(25) = 91: a1(26) = 92: a1(27) = 104: a1(28) = 105: a1(29) = 117: a1(30) = 118: a1(31) = 130: a1(32) = 131:
a1(33) = 143: a1(34) = 144: a1(35) = 156: a1(36) = 157: a1(37) = 158: a1(38) = 159: a1(39) = 160: a1(40) = 161:
a1(41) = 162: a1(42) = 163: a1(43) = 164: a1(44) = 165: a1(45) = 166: a1(46) = 167: a1(47) = 168: a1(48) = 169:
For i1 = m1 To m2
b1(a1(i1)) = a1(i1)
Next i1
' P14 Option 1
'' a(1) = 169: a(7) = 7: a(13) = 157:
'' a(79) = 79: a(85) = 85: a(91) = 91:
'' a(157) = 13: a(163) = 163: a(169) = 1:
' P14 Option 2
a(1) = 168: a(7) = 8: a(13) = 156:
a(79) = 79: a(85) = 85: a(91) = 91:
a(157) = 14: a(163) = 162: a(169) = 2:
' Block Key Variables
b(a(1)) = a(1): b(a(7)) = a(7): b(a(13)) = a(13):
b(a(79)) = a(79): b(a(85)) = a(85): b(a(91)) = a(91):
b(a(157)) = a(157): b(a(163)) = a(163): b(a(169)) = a(169):
' Generate Borders
Sheets("Klad1").Select
t1 = Timer
For j168 = m2 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) = Pr13 - a(168): If b(a(12)) = 0 Then b(a(12)) = a(12): c(12) = a(12) Else GoTo 120
For j167 = m2 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) = Pr13 - a(167): If b(a(11)) = 0 Then b(a(11)) = a(11): c(11) = a(11) Else GoTo 110
For j166 = m2 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) = Pr13 - a(166): If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 100
For j165 = m2 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) = Pr13 - a(165): If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 90
For j164 = m2 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) = Pr13 - a(164): If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 80
For j162 = m1 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) = Pr13 - a(162): If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 60
For j161 = m1 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) = Pr13 - a(161): If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 50
For j160 = m1 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) = Pr13 - a(160): If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 40
For j159 = m1 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) = Pr13 - 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) = Pr13 - 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) = Pr13 - a(156): If b(a(144)) = 0 Then b(a(144)) = a(144): c(144) = a(144) Else GoTo 1440
For j143 = m2 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) = Pr13 - a(143): If b(a(131)) = 0 Then b(a(131)) = a(131): c(131) = a(131) Else GoTo 1310
For j130 = m2 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) = Pr13 - a(130): If b(a(118)) = 0 Then b(a(118)) = a(118): c(118) = a(118) Else GoTo 1180
For j117 = m2 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) = Pr13 - a(117): If b(a(105)) = 0 Then b(a(105)) = a(105): c(105) = a(105) Else GoTo 1050
For j104 = m2 To m1 Step -1 '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) = Pr13 - 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) = Pr13 - 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) = Pr13 - 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) = Pr13 - 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) = Pr13 - 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) = Pr13 - a(26): If b(a(14)) = 0 Then b(a(14)) = a(14): c(14) = a(14) Else GoTo 140
n9 = n9 + 1: GoSub 2650: End
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(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
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s1)
y = MsgBox(t10, 0, "Routine Priem13a")
End
' Print results (lines)
2640 Cells(n9, 170).Select
For i1 = 1 To 169
Cells(n9, i1).Value = a(i1)
Next i1
Cells(n9, 170).Value = s1
Return
' Print results (squares)
2650 n2 = n2 + 1
If n2 = 3 Then
n2 = 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 = n9
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
End Sub