' Constructs Borders for 14 x 14 Concentric Magic Squares (Distinct Integers)
' Tested with Office 365 under Windows 10
Sub MgcSqrs14a()
Dim a(196), a1(52), b(196), b1(196), c(196)
y = MsgBox("Blocked", vbInformation, "MgcSqrs14a")
End
For i1 = 1 To 26: a1(i1) = i1: Next i1
For i1 = 1 To 26: a1(i1 + 26) = i1 + 170: Next i1
For i1 = 1 To 52
b1(a1(i1)) = a1(i1)
Next i1
k1 = 1: k2 = 1
m1 = 1: m2 = 52: s1 = 1379: p2 = 197
t1 = Timer
Sheets("Klad1").Select
' Generate Border
For j196 = m2 To m1 Step -1 'a(196)
If b(a1(j196)) = 0 Then b(a1(j196)) = a1(j196): c(196) = a1(j196) Else GoTo 1960
a(196) = a1(j196)
a(1) = p2 - a(196): If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 10
For j183 = j196 To m1 Step -1 'a(183)
If b(a1(j183)) = 0 Then b(a1(j183)) = a1(j183): c(183) = a1(j183) Else GoTo 1830
a(183) = a1(j183)
a(14) = p2 - a(183): If b(a(14)) = 0 Then b(a(14)) = a(14): c(14) = a(14) Else GoTo 140
Cells(2, 1).Value = j183
For j195 = m2 To m1 Step -1 'a(195)
If b(a1(j195)) = 0 Then b(a1(j195)) = a1(j195): c(195) = a1(j195) Else GoTo 1950
a(195) = a1(j195)
a(13) = p2 - a(195): If b(a(13)) = 0 Then b(a(13)) = a(13): c(13) = a(13) Else GoTo 130
Cells(3, 1).Value = j195
For j194 = j195 To m1 Step -1 'a(194)
If b(a1(j194)) = 0 Then b(a1(j194)) = a1(j194): c(194) = a1(j194) Else GoTo 1940
a(194) = a1(j194)
a(12) = p2 - a(194): If b(a(12)) = 0 Then b(a(12)) = a(12): c(12) = a(12) Else GoTo 120
For j193 = j194 To m1 Step -1 'a(193)
If b(a1(j193)) = 0 Then b(a1(j193)) = a1(j193): c(193) = a1(j193) Else GoTo 1930
a(193) = a1(j193)
a(11) = p2 - a(193): If b(a(11)) = 0 Then b(a(11)) = a(11): c(11) = a(11) Else GoTo 110
For j192 = j193 To m1 Step -1 'a(192)
If b(a1(j192)) = 0 Then b(a1(j192)) = a1(j192): c(192) = a1(j192) Else GoTo 1920
a(192) = a1(j192)
a(10) = p2 - a(192): If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 100
For j191 = j192 To m1 Step -1 'a(191)
If b(a1(j191)) = 0 Then b(a1(j191)) = a1(j191): c(191) = a1(j191) Else GoTo 1910
a(191) = a1(j191)
a(9) = p2 - a(191): If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 90
For j190 = j191 To m1 Step -1 'a(190)
If b(a1(j190)) = 0 Then b(a1(j190)) = a1(j190): c(190) = a1(j190) Else GoTo 1900
a(190) = a1(j190)
a(8) = p2 - a(190): If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 80
For j189 = j190 To m1 Step -1 'a(189)
If b(a1(j189)) = 0 Then b(a1(j189)) = a1(j189): c(189) = a1(j189) Else GoTo 1890
a(189) = a1(j189)
a(7) = p2 - a(189): If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 70
For j188 = j189 To m1 Step -1 'a(188)
If b(a1(j188)) = 0 Then b(a1(j188)) = a1(j188): c(188) = a1(j188) Else GoTo 1880
a(188) = a1(j188)
a(6) = p2 - a(188): If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 60
For j187 = j188 To m1 Step -1 'a(187)
If b(a1(j187)) = 0 Then b(a1(j187)) = a1(j187): c(187) = a1(j187) Else GoTo 1870
a(187) = a1(j187)
a(5) = p2 - a(187): If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 50
For j186 = j187 To m1 Step -1 'a(186)
If b(a1(j186)) = 0 Then b(a1(j186)) = a1(j186): c(186) = a1(j186) Else GoTo 1860
a(186) = a1(j186)
a(4) = p2 - a(186): If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 40
For j185 = j186 To m1 Step -1 'a(185)
If b(a1(j185)) = 0 Then b(a1(j185)) = a1(j185): c(185) = a1(j185) Else GoTo 1850
a(185) = a1(j185)
a(3) = p2 - a(185): If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 30
a(184)= s1-a(183)-a(185)-a(186)-a(187)-a(188)-a(189)-a(190)-a(191)-a(192)-a(193)-a(194)-a(195)-a(196)
If a(184) < a1(m1) Or a(184) > a1(m2) Then GoTo 1840
If b1(a(184)) = 0 Then GoTo 1840
If a(184) > a(185) Then GoTo 1840
If b(a(184)) = 0 Then b(a(184)) = a(184): c(184) = a(184) Else GoTo 1840
a(2) = p2 - a(184): If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 20
' Columns
For j182 = m2 To m1 Step -1 'a(182)
If b(a1(j182)) = 0 Then b(a1(j182)) = a1(j182): c(182) = a1(j182) Else GoTo 1820
a(182) = a1(j182)
a(169) = p2 - a(182): If b(a(169)) = 0 Then b(a(169)) = a(169): c(169) = a(169) Else GoTo 1690
For j168 = j182 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(155) = p2 - a(168): If b(a(155)) = 0 Then b(a(155)) = a(155): c(155) = a(155) Else GoTo 1550
For j154 = j168 To m1 Step -1 'a(154)
If b(a1(j154)) = 0 Then b(a1(j154)) = a1(j154): c(154) = a1(j154) Else GoTo 1540
a(154) = a1(j154)
a(141) = p2 - a(154): If b(a(141)) = 0 Then b(a(141)) = a(141): c(141) = a(141) Else GoTo 1410
For j140 = j154 To m1 Step -1 'a(140)
If b(a1(j140)) = 0 Then b(a1(j140)) = a1(j140): c(140) = a1(j140) Else GoTo 1400
a(140) = a1(j140)
a(127) = p2 - a(140): If b(a(127)) = 0 Then b(a(127)) = a(127): c(127) = a(127) Else GoTo 1270
For j126 = j140 To m1 Step -1 'a(126)
If b(a1(j126)) = 0 Then b(a1(j126)) = a1(j126): c(126) = a1(j126) Else GoTo 1260
a(126) = a1(j126)
a(113) = p2 - a(126): If b(a(113)) = 0 Then b(a(113)) = a(113): c(113) = a(113) Else GoTo 1130
For j112 = j126 To m1 Step -1 'a(112)
If b(a1(j112)) = 0 Then b(a1(j112)) = a1(j112): c(112) = a1(j112) Else GoTo 1120
a(112) = a1(j112)
a(99) = p2 - a(112): If b(a(99)) = 0 Then b(a(99)) = a(99): c(99) = a(99) Else GoTo 990
For j98 = j112 To m1 Step -1 'a(98)
If b(a1(j98)) = 0 Then b(a1(j98)) = a1(j98): c(98) = a1(j98) Else GoTo 980
a(98) = a1(j98)
a(85) = p2 - a(98): If b(a(85)) = 0 Then b(a(85)) = a(85): c(85) = a(85) Else GoTo 850
For j84 = j98 To m1 Step -1 'a(84)
If b(a1(j84)) = 0 Then b(a1(j84)) = a1(j84): c(84) = a1(j84) Else GoTo 840
a(84) = a1(j84)
a(71) = p2 - a(84): If b(a(71)) = 0 Then b(a(71)) = a(71): c(71) = a(71) Else GoTo 710
For j70 = j84 To m1 Step -1 'a(70)
If b(a1(j70)) = 0 Then b(a1(j70)) = a1(j70): c(70) = a1(j70) Else GoTo 700
a(70) = a1(j70)
a(57) = p2 - a(70): If b(a(57)) = 0 Then b(a(57)) = a(57): c(57) = a(57) Else GoTo 570
For j56 = j70 To m1 Step -1 'a(56)
If b(a1(j56)) = 0 Then b(a1(j56)) = a1(j56): c(56) = a1(j56) Else GoTo 560
a(56) = a1(j56)
a(43) = p2 - a(56): If b(a(43)) = 0 Then b(a(43)) = a(43): c(43) = a(43) Else GoTo 430
For j42 = j56 To m1 Step -1 'a(42)
If b(a1(j42)) = 0 Then b(a1(j42)) = a1(j42): c(42) = a1(j42) Else GoTo 420
a(42) = a1(j42)
a(29) = p2 - a(42): If b(a(29)) = 0 Then b(a(29)) = a(29): c(29) = a(29) Else GoTo 290
a(28)= s1-a(14)-a(42)-a(56)-a(70)-a(84)-a(98)-a(112)-a(126)-a(140)-a(154)-a(168)-a(182)-a(196)
If a(28) < a1(m1) Or a(28) > a1(m2) Then GoTo 280
If b1(a(28)) = 0 Then GoTo 280
If a(28) > a(42) Then GoTo 280
If b(a(28)) = 0 Then b(a(28)) = a(28): c(28) = a(28) Else GoTo 280
a(15) = p2 - a(28): If b(a(15)) = 0 Then b(a(15)) = a(15): c(15) = a(15) Else GoTo 150
' Exclude solutions with identical numbers (Back Check)
GoSub 800: If fl1 = 0 Then GoTo 5
' n9 = n9 + 1: GoSub 1640 'Print results (selected numbers)
n9 = n9 + 1: GoSub 1650 'Print results (squares)
' n9 = n9 + 1: Cells(1, 1).Value = n9 'counting
Erase b, c: GoTo 1960 'Print only first square for j196
5
b(c(15)) = 0: c(15) = 0
150 b(c(28)) = 0: c(28) = 0
280 b(c(29)) = 0: c(29) = 0
290 b(c(42)) = 0: c(42) = 0
420 Next j42
b(c(43)) = 0: c(43) = 0
430 b(c(56)) = 0: c(56) = 0
560 Next j56
b(c(57)) = 0: c(57) = 0
570 b(c(70)) = 0: c(70) = 0
700 Next j70
b(c(71)) = 0: c(71) = 0
710 b(c(84)) = 0: c(84) = 0
840 Next j84
b(c(85)) = 0: c(85) = 0
850 b(c(98)) = 0: c(98) = 0
980 Next j98
b(c(99)) = 0: c(99) = 0
990 b(c(112)) = 0: c(112) = 0
1120 Next j112
b(c(113)) = 0: c(113) = 0
1130 b(c(126)) = 0: c(126) = 0
1260 Next j126
b(c(127)) = 0: c(127) = 0
1270 b(c(140)) = 0: c(140) = 0
1400 Next j140
b(c(141)) = 0: c(141) = 0
1410 b(c(154)) = 0: c(154) = 0
1540 Next j154
b(c(155)) = 0: c(155) = 0
1550 b(c(168)) = 0: c(168) = 0
1680 Next j168
b(c(169)) = 0: c(169) = 0
1690 b(c(182)) = 0: c(182) = 0
1820 Next j182
b(c(2)) = 0: c(2) = 0
20 b(c(184)) = 0: c(184) = 0
1840 b(c(3)) = 0: c(3) = 0
30 b(c(185)) = 0: c(185) = 0
1850 Next j185
b(c(4)) = 0: c(4) = 0
40 b(c(186)) = 0: c(186) = 0
1860 Next j186
b(c(5)) = 0: c(5) = 0
50 b(c(187)) = 0: c(187) = 0
1870 Next j187
b(c(6)) = 0: c(6) = 0
60 b(c(188)) = 0: c(188) = 0
1880 Next j188
b(c(7)) = 0: c(7) = 0
70 b(c(189)) = 0: c(189) = 0
1890 Next j189
b(c(8)) = 0: c(8) = 0
80 b(c(190)) = 0: c(190) = 0
1900 Next j190
b(c(9)) = 0: c(9) = 0
90 b(c(191)) = 0: c(191) = 0
1910 Next j191
b(c(10)) = 0: c(10) = 0
100 b(c(192)) = 0: c(192) = 0
1920 Next j192
b(c(11)) = 0: c(11) = 0
110 b(c(193)) = 0: c(193) = 0
1930 Next j193
b(c(12)) = 0: c(12) = 0
120 b(c(194)) = 0: c(194) = 0
1940 Next j194
b(c(13)) = 0: c(13) = 0
130 b(c(195)) = 0: c(195) = 0
1950 Next j195
b(c(14)) = 0: c(14) = 0
140 b(c(183)) = 0: c(183) = 0
1830 Next j183
b(c(1)) = 0: c(1) = 0
10 b(c(196)) = 0: c(196) = 0
1960 Next j196
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s1)
y = MsgBox(t10, 0, "Routine MgcSqrs14a")
End
' Print results (selected numbers)
1640 For i1 = 1 To 196
Cells(n9, i1).Value = a(i1)
Next i1
Cells(n9, 170).Value = n9
Return
' Print results (squares)
1650 n2 = n2 + 1
If n2 = 3 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 = n9
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
' Exclude solutions with identical numbers (Back Check)
800 fl1 = 1
For j1 = 1 To 196
a2 = a(j1): If a2 = 0 Then GoTo 810
For j2 = (1 + j1) To 196
If a2 = a(j2) Then fl1 = 0: Return
Next j2
810 Next j1
Return
End Sub