' Generates Inlaid Magic Squares of order 13, Integers 1 ... 169, Part I (Border)
' Tested with Office 365 under Windows 10
Sub MgcSqr13k1()
Dim a1(169), a(169), b1(169), b(169), c(169), s(4)
Dim a11(121)
y = MsgBox("Locked", vbCritical, "Routine MgcSqr13k1")
End
n1 = 0: n2 = 0: n3 = 0: k1 = 1: k2 = 1: n9 = 0: n10 = 0
s1 = 1105: Pr13 = 170
' Generate Squares
Sheets("Klad1").Select
t1 = Timer
For j100 = 2 To 2
Cells(1, 1).Value = j100
s(3) = Sheets("InpLns5").Cells(j100, 124).Value
s(4) = Sheets("InpLns5").Cells(j100, 125).Value
s(1) = 12 * s1 / 13 - s(4)
s(2) = 10 * s1 / 13 - s(3)
For i1 = 1 To 169 'Redefine Integers
b1(i1) = i1
a1(i1) = i1
Next i1
m1 = 1: m2 = 169
' Create Border
t11 = Timer 'Time Out
For j169 = m1 To m2 'a(169)
Cells(2, 1).Value = j169
If b(a1(j169)) = 0 Then b(a1(j169)) = a1(j169): c(169) = a1(j169) Else GoTo 1690
a(169) = a1(j169)
a(1) = 2 * s1 / 13 - a(169):
If b1(a(1)) = 0 Then GoTo 10
If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 10
For j168 = m1 To m2 '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(158) = -s1 / 13 + a(168) - s(3) + s(4)
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(12) = 2 * s1 / 13 - a(158):
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(2) = 2 * s1 / 13 - a(168):
If b1(a(2)) = 0 Then GoTo 20
If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 20
For j167 = j168 + 1 To m2 '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(159) = -s1 / 13 + a(167) - s(3) + s(4)
If a(159) < a1(m1) Or a(159) > a1(m2) Then GoTo 1590
If b1(a(159)) = 0 Then GoTo 1590
If b(a(159)) = 0 Then b(a(159)) = a(159): c(159) = a(159) Else GoTo 1590
a(11) = 2 * s1 / 13 - a(159):
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(3) = 2 * s1 / 13 - a(167):
If b1(a(3)) = 0 Then GoTo 30
If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 30
For j166 = j167 + 1 To m2 '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(160) = -s1 / 13 + a(166) - s(3) + s(4)
If a(160) < a1(m1) Or a(160) > a1(m2) Then GoTo 1600
If b1(a(160)) = 0 Then GoTo 1600
If b(a(160)) = 0 Then b(a(160)) = a(160): c(160) = a(160) Else GoTo 1600
a(10) = 2 * s1 / 13 - a(160):
If b1(a(10)) = 0 Then GoTo 100
If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 100
a(4) = 2 * s1 / 13 - a(166):
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
For j165 = j166 + 1 To m2 '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(161) = -s1 / 13 + a(165) - s(3) + s(4)
If a(161) < a1(m1) Or a(161) > a1(m2) Then GoTo 1610
If b1(a(161)) = 0 Then GoTo 1610
If b(a(161)) = 0 Then b(a(161)) = a(161): c(161) = a(161) Else GoTo 1610
a(9) = 2 * s1 / 13 - a(161):
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(5) = 2 * s1 / 13 - a(165):
If b1(a(5)) = 0 Then GoTo 50
If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 50
For j164 = j165 + 1 To m2 '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(162) = -s1 / 13 + a(164) - s(3) + s(4)
If a(162) < a1(m1) Or a(162) > a1(m2) Then GoTo 1620
If b1(a(162)) = 0 Then GoTo 1620
If b(a(162)) = 0 Then b(a(162)) = a(162): c(162) = a(162) Else GoTo 1620
a(8) = 2 * s1 / 13 - a(162):
If b1(a(8)) = 0 Then GoTo 80
If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 80
a(6) = 2 * s1 / 13 - a(164):
If b1(a(6)) = 0 Then GoTo 60
If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 60
For j163 = j164 + 1 To m2 'a(163)
If b(a1(j163)) = 0 Then b(a1(j163)) = a1(j163): c(163) = a1(j163) Else GoTo 1630
a(163) = a1(j163)
a(157) = 18 * s1 / 13 - a(163) - 2*a(164) - 2*a(165) - 2*a(166) - 2*a(167) - 2*a(168) - a(169) + 5*s(3) - 5*s(4)
If a(157) < a1(m1) Or a(157) > a1(m2) Then GoTo 1570
If b1(a(157)) = 0 Then GoTo 1570
If b(a(157)) = 0 Then b(a(157)) = a(157): c(157) = a(157) Else GoTo 1570
a(13) = 2 * s1 / 13 - a(157):
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(7) = 2 * s1 / 13 - a(163):
If b1(a(7)) = 0 Then GoTo 70
If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 70
For j156 = m1 To m2 '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) = 13 * s1 / 13 - a(156) - s(3) - s(4)
If a(144) < a1(m1) Or a(144) > a1(m2) Then GoTo 1440
If b1(a(144)) = 0 Then GoTo 1440
If b(a(144)) = 0 Then b(a(144)) = a(144): c(144) = a(144) Else GoTo 1440
a(26) = 2 * s1 / 13 - a(144):
If b1(a(26)) = 0 Then GoTo 260
If b(a(26)) = 0 Then b(a(26)) = a(26): c(14) = a(26) Else GoTo 260
a(14) = 2 * s1 / 13 - a(156):
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
For j143 = j156 + 1 To m2 '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) = 13 * s1 / 13 - a(143) - s(3) - s(4)
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(39) = 2 * s1 / 13 - a(131):
If b1(a(39)) = 0 Then GoTo 390
If b(a(39)) = 0 Then b(a(39)) = a(39): c(39) = a(39) Else GoTo 390
a(27) = 2 * s1 / 13 - a(143):
If b1(a(27)) = 0 Then GoTo 270
If b(a(27)) = 0 Then b(a(27)) = a(27): c(27) = a(27) Else GoTo 270
For j130 = j143 + 1 To m2 '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) = 13 * s1 / 13 - a(130) - s(3) - s(4)
If a(118) < a1(m1) Or a(118) > a1(m2) Then GoTo 1180
If b1(a(118)) = 0 Then GoTo 1180
If b(a(118)) = 0 Then b(a(118)) = a(118): c(118) = a(118) Else GoTo 1180
a(52) = 2 * s1 / 13 - a(118):
If b1(a(52)) = 0 Then GoTo 520
If b(a(52)) = 0 Then b(a(52)) = a(52): c(52) = a(52) Else GoTo 520
a(40) = 2 * s1 / 13 - a(130):
If b1(a(40)) = 0 Then GoTo 40
If b(a(40)) = 0 Then b(a(40)) = a(40): c(40) = a(40) Else GoTo 400
For j117 = j130 + 1 To m2 '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) = 13 * s1 / 13 - a(117) - s(3) - s(4)
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(65) = 2 * s1 / 13 - a(105):
If b1(a(65)) = 0 Then GoTo 650
If b(a(65)) = 0 Then b(a(65)) = a(65): c(65) = a(65) Else GoTo 650
a(53) = 2 * s1 / 13 - a(117):
If b1(a(53)) = 0 Then GoTo 530
If b(a(53)) = 0 Then b(a(53)) = a(53): c(53) = a(53) Else GoTo 530
For j104 = j117 + 1 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) = 13 * s1 / 13 - a(104) - s(3) - s(4)
If a(92) < a1(m1) Or a(92) > a1(m2) Then GoTo 920
If b1(a(92)) = 0 Then GoTo 920
If b(a(92)) = 0 Then b(a(92)) = a(92): c(92) = a(92) Else GoTo 920
a(91) = 66 * s1 / 13 - 2*a(104) - 2*a(117) - 2*a(130) - 2*a(143) - 2*a(156) + a(157) - a(169) - 5*s(3) - 5*s(4)
If a(91) < a1(m1) Or a(91) > a1(m2) Then GoTo 910
If b1(a(91)) = 0 Then GoTo 910
If b(a(91)) = 0 Then b(a(91)) = a(91): c(91) = a(91) Else GoTo 910
a(79) = 2 * s1 / 13 - a(91):
If b1(a(79)) = 0 Then GoTo 790
If b(a(79)) = 0 Then b(a(79)) = a(79): c(79) = a(79) Else GoTo 790
a(78) = 2 * s1 / 13 - a(92):
If b1(a(78)) = 0 Then GoTo 780
If b(a(78)) = 0 Then b(a(78)) = a(78): c(78) = a(78) Else GoTo 780
a(66) = 2 * s1 / 13 - a(104):
If b1(a(66)) = 0 Then GoTo 660
If b(a(66)) = 0 Then b(a(66)) = a(66): c(66) = a(66) Else GoTo 660
GoSub 1500: If fl1 = 0 Then GoTo 5 'Back Check Identical Numbers
n9 = n9 + 1: GoSub 2650 'Print Square
'' n9 = n9 + 1: Cells(1, 1).Value = n9 'Counting
'' Erase b, c: GoTo 500 'Print only one square for j100
Erase b, c: GoTo 1690 'Print only one square for j169
5
b(c(66)) = 0: c(66) = 0
660 b(c(78)) = 0: c(78) = 0
780 b(c(79)) = 0: c(79) = 0
790 b(c(91)) = 0: c(91) = 0
910 b(c(92)) = 0: c(92) = 0
920 b(c(104)) = 0: c(104) = 0
1040 Next j104
b(c(53)) = 0: c(53) = 0
530 b(c(65)) = 0: c(65) = 0
650 b(c(105)) = 0: c(105) = 0
1050 b(c(117)) = 0: c(117) = 0
1170 Next j117
b(c(40)) = 0: c(40) = 0
400 b(c(52)) = 0: c(52) = 0
520 b(c(118)) = 0: c(118) = 0
1180 b(c(130)) = 0: c(130) = 0
1300 Next j130
b(c(27)) = 0: c(27) = 0
270 b(c(39)) = 0: c(39) = 0
390 b(c(131)) = 0: c(131) = 0
1310 b(c(143)) = 0: c(143) = 0
1430 Next j143
b(c(14)) = 0: c(14) = 0
140 b(c(26)) = 0: c(26) = 0
260 b(c(144)) = 0: c(144) = 0
1440 b(c(156)) = 0: c(156) = 0
1560 Next j156
b(c(7)) = 0: c(7) = 0
70 b(c(13)) = 0: c(13) = 0
130 b(c(157)) = 0: c(157) = 0
1570 b(c(163)) = 0: c(163) = 0
1630 Next j163
b(c(6)) = 0: c(6) = 0
60 b(c(8)) = 0: c(8) = 0
80 b(c(162)) = 0: c(162) = 0
1620 b(c(164)) = 0: c(164) = 0
1640 Next j164
b(c(5)) = 0: c(5) = 0
50 b(c(9)) = 0: c(9) = 0
90 b(c(161)) = 0: c(161) = 0
1610 b(c(165)) = 0: c(165) = 0
1650 Next j165
b(c(4)) = 0: c(4) = 0
40 b(c(10)) = 0: c(10) = 0
100 b(c(160)) = 0: c(160) = 0
1600 b(c(166)) = 0: c(166) = 0
1660 Next j166
b(c(3)) = 0: c(3) = 0
30 b(c(11)) = 0: c(11) = 0
110 b(c(159)) = 0: c(159) = 0
1590 b(c(167)) = 0: c(167) = 0
1670 Next j167
b(c(2)) = 0: c(2) = 0
20 b(c(12)) = 0: c(12) = 0
120 b(c(158)) = 0: c(158) = 0
1580 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
Erase b, c
500 Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s11)
y = MsgBox(t10, 0, "Routine MgcSqr13k1")
End
' Exclude solutions with identical numbers
1500 fl1 = 1
For i10 = 1 To 169
c2 = a(i10): If c2 = 0 Then GoTo 1510
For i20 = (1 + i10) To 169
If c2 = a(i20) Then fl1 = 0: Return
Next i20
1510 Next i10
Return
' Print results (lines)
2640
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 k1 = k1 + 14
End If
Cells(2, 1).Value = n9
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = j100
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