' Constructs 13 x 13 Composed Magic Squares for Natural Numbers (Part 1)
' Associated Magic Corner Squares and - Rectangles
' Tested with Office 365 under Windows 10
Sub Prime13c1()
Dim a1(169), a(169), a13(169), b1(169), b(169), c(169)
y = MsgBox("Locked", vbCritical, "Routine Prime13c1")
End
Sheets("Klad1").Select
n5 = 0: n9 = 0: k1 = 1: k2 = 1
ShtNm2 = "Lines6"
Pr3 = 170: Cntr3 = 85 'Center Element
s6 = 6 * Cntr3 'MC6
s13 = 13 * Cntr3 'MC13
t1 = Timer
For j101 = 1 To 128
' Redefine variables
Erase b1
For j1 = 1 To 169
a1(j1) = j1: b1(j1) = j1
Next j1
pmax = 169: m1 = 1: m2 = 169
' Read Corner Square 6 x 6
For i1 = 1 To 36
a(i1) = Sheets(ShtNm2).Cells(j101, i1).Value
Next i1
n10 = 0: n53 = 36: GoSub 910 'Remove used integers from available integers
Erase a13
a13(1) = a(1): a13(2) = a(2): a13(3) = a(3): a13(4) = a(4): a13(5) = a(5): a13(6) = a(6):
a13(14) = a(7): a13(15) = a(8): a13(16) = a(9): a13(17) = a(10): a13(18) = a(11): a13(19) = a(12):
a13(27) = a(13): a13(28) = a(14): a13(29) = a(15): a13(30) = a(16): a13(31) = a(17): a13(32) = a(18):
a13(40) = a(19): a13(41) = a(20): a13(42) = a(21): a13(43) = a(22): a13(44) = a(23): a13(45) = a(24):
a13(53) = a(25): a13(54) = a(26): a13(55) = a(27): a13(56) = a(28): a13(57) = a(29): a13(58) = a(30):
a13(66) = a(31): a13(67) = a(32): a13(68) = a(33): a13(69) = a(34): a13(70) = a(35): a13(71) = a(36):
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
' Generate Border Sections 6 x 7 (2 ea)
1200 n10 = 0 'Continue
b(Cntr3) = Cntr3 'Block Center Element
For j42 = m1 To m2 / 2 'a(42)
If b1(a1(j42)) = 0 Then GoTo 420
If b(a1(j42)) = 0 Then b(a1(j42)) = a1(j42): c(42) = a1(j42) Else GoTo 420
a(42) = a1(j42)
a(1) = Pr3 - a(42): If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 10
For j41 = m1 To m2 'a(41)
If b1(a1(j41)) = 0 Then GoTo 410
If b(a1(j41)) = 0 Then b(a1(j41)) = a1(j41): c(41) = a1(j41) Else GoTo 410
a(41) = a1(j41)
a(40) = s6 / 2 - a(41) - a(42)
If a(40) < a1(m1) Or a(40) > a1(m2) Then GoTo 400
If b1(a(40)) = 0 Then GoTo 400
If b(a(40)) = 0 Then b(a(40)) = a(40): c(40) = a(40) Else GoTo 400
a(3) = Pr3 - a(40): If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 30
a(2) = Pr3 - a(41): If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 20
For j36 = m1 To m2 'a(36)
If b1(a1(j36)) = 0 Then GoTo 360
If b(a1(j36)) = 0 Then b(a1(j36)) = a1(j36): c(36) = a1(j36) Else GoTo 360
a(36) = a1(j36)
a(30) = s6 / 2 - a(36) - a(42)
If a(30) < a1(m1) Or a(30) > a1(m2) Then GoTo 300
If b1(a(30)) = 0 Then GoTo 300
If b(a(30)) = 0 Then b(a(30)) = a(30): c(30) = a(30) Else GoTo 300
a(13) = Pr3 - a(30): If b(a(13)) = 0 Then b(a(13)) = a(13): c(13) = a(13) Else GoTo 130
a(7) = Pr3 - a(36): If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 70
For j35 = m1 To m2 'a(35)
If b1(a1(j35)) = 0 Then GoTo 350
If b(a1(j35)) = 0 Then b(a1(j35)) = a1(j35): c(35) = a1(j35) Else GoTo 350
a(35) = a1(j35)
a(34) = s6 / 2 - a(35) - a(36)
If a(34) < a1(m1) Or a(34) > a1(m2) Then GoTo 340
If b1(a(34)) = 0 Then GoTo 340
If b(a(34)) = 0 Then b(a(34)) = a(34): c(34) = a(34) Else GoTo 340
a(29) = s6 / 2 - a(35) - a(41)
If a(29) < a1(m1) Or a(29) > a1(m2) Then GoTo 290
If b1(a(29)) = 0 Then GoTo 290
If b(a(29)) = 0 Then b(a(29)) = a(29): c(29) = a(29) Else GoTo 290
a(28) = -s6 / 2 + a(35) + a(36) + a(41) + a(42)
If a(28) < a1(m1) Or a(28) > a1(m2) Then GoTo 280
If b1(a(28)) = 0 Then GoTo 280
If b(a(28)) = 0 Then b(a(28)) = a(28): c(28) = a(28) Else GoTo 280
a(15) = Pr3 - a(28): If b(a(15)) = 0 Then b(a(15)) = a(15): c(15) = a(15) Else GoTo 150
a(14) = Pr3 - a(29): If b(a(14)) = 0 Then b(a(14)) = a(14): c(14) = a(14) Else GoTo 140
a(9) = Pr3 - a(34): If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 90
a(8) = Pr3 - a(35): If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 80
For j39 = m1 To m2 'a(39)
If b1(a1(j39)) = 0 Then GoTo 390
If b(a1(j39)) = 0 Then b(a1(j39)) = a1(j39): c(39) = a1(j39) Else GoTo 390
a(39) = a1(j39)
a(4) = Pr3 - a(39): If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 40
For j38 = m1 To m2 'a(38)
If b1(a1(j38)) = 0 Then GoTo 380
If b(a1(j38)) = 0 Then b(a1(j38)) = a1(j38): c(38) = a1(j38) Else GoTo 380
a(38) = a1(j38)
a(37) = s6 / 2 - a(38) - a(39)
If a(37) < a1(m1) Or a(37) > a1(m2) Then GoTo 370
If b1(a(37)) = 0 Then GoTo 370
If b(a(37)) = 0 Then b(a(37)) = a(37): c(37) = a(37) Else GoTo 370
a(6) = Pr3 - a(37): If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 60
a(5) = Pr3 - a(38): If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 50
For j33 = m1 To m2 'a(33)
If b1(a1(j33)) = 0 Then GoTo 330
If b(a1(j33)) = 0 Then b(a1(j33)) = a1(j33): c(33) = a1(j33) Else GoTo 330
a(33) = a1(j33)
a(10) = Pr3 - a(33): If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 100
For j32 = m1 To m2 'a(32)
If b1(a1(j32)) = 0 Then GoTo 320
If b(a1(j32)) = 0 Then b(a1(j32)) = a1(j32): c(32) = a1(j32) Else GoTo 320
a(32) = a1(j32)
a(31) = s6 / 2 - a(32) - a(33)
If a(31) < a1(m1) Or a(31) > a1(m2) Then GoTo 310
If b1(a(31)) = 0 Then GoTo 310
If b(a(31)) = 0 Then b(a(31)) = a(31): c(31) = a(31) Else GoTo 310
a(12) = Pr3 - a(31): If b(a(12)) = 0 Then b(a(12)) = a(12): c(12) = a(12) Else GoTo 120
a(11) = Pr3 - a(32): If b(a(11)) = 0 Then b(a(11)) = a(11): c(11) = a(11) Else GoTo 110
For j27 = m1 To m2 'a(27)
If b1(a1(j27)) = 0 Then GoTo 270
If b(a1(j27)) = 0 Then b(a1(j27)) = a1(j27): c(27) = a1(j27) Else GoTo 270
a(27) = a1(j27)
a(22) = -s6 / 3 + a(27) + a(33) + a(39)
If a(22) < a1(m1) Or a(22) > a1(m2) Then GoTo 220
If b1(a(22)) = 0 Then GoTo 220
If b(a(22)) = 0 Then b(a(22)) = a(22): c(22) = a(22) Else GoTo 220
a(21) = Pr3 - a(22): If b(a(21)) = 0 Then b(a(21)) = a(21): c(21) = a(21) Else GoTo 210
a(16) = Pr3 - a(27): If b(a(16)) = 0 Then b(a(16)) = a(16): c(16) = a(16) Else GoTo 160
For j26 = m1 To m2 'a(26)
If b1(a1(j26)) = 0 Then GoTo 260
If b(a1(j26)) = 0 Then b(a1(j26)) = a1(j26): c(26) = a1(j26) Else GoTo 260
a(26) = a1(j26)
a(25) = s6 / 2 - a(26) - a(27)
If a(25) < a1(m1) Or a(25) > a1(m2) Then GoTo 250
If b1(a(25)) = 0 Then GoTo 250
If b(a(25)) = 0 Then b(a(25)) = a(25): c(25) = a(25) Else GoTo 250
a(24) = 7 * s6 / 6 - a(26) - a(27) - a(32) - a(33) - a(38) - a(39)
If a(24) < a1(m1) Or a(24) > a1(m2) Then GoTo 240
If b1(a(24)) = 0 Then GoTo 240
If b(a(24)) = 0 Then b(a(24)) = a(24): c(24) = a(24) Else GoTo 240
a(23) = -s6 / 3 + a(26) + a(32) + a(38)
If a(23) < a1(m1) Or a(23) > a1(m2) Then GoTo 230
If b1(a(23)) = 0 Then GoTo 230
If b(a(23)) = 0 Then b(a(23)) = a(23): c(23) = a(23) Else GoTo 230
a(20) = Pr3 - a(23): If b(a(20)) = 0 Then b(a(20)) = a(20): c(20) = a(20) Else GoTo 200
a(19) = Pr3 - a(24): If b(a(19)) = 0 Then b(a(19)) = a(19): c(19) = a(19) Else GoTo 190
a(18) = Pr3 - a(25): If b(a(18)) = 0 Then b(a(18)) = a(18): c(18) = a(18) Else GoTo 180
a(17) = Pr3 - a(26): If b(a(17)) = 0 Then b(a(17)) = a(17): c(17) = a(17) Else GoTo 170
If n10 = 1 Then
a13(85) = s13 - a13(157) - a13(145) - a13(133) - a13(121) - a13(109) - a13(97) - a(12) - a(17) - a(22) - a(27) - a(32) - a(37)
If a13(85) < a1(m1) Or a13(85) > a1(m2) Then GoTo 5:
If b1(a13(85)) = 0 Then GoTo 5
If b(a13(85)) <> 0 Then GoTo 5
End If
n10 = n10 + 1
If n10 = 1 Then
a13(79) = a(1): a13(80) = a(2): a13(81) = a(3): a13(82) = a(4): a13(83) = a(5): a13(84) = a(6):
a13(92) = a(7): a13(93) = a(8): a13(94) = a(9): a13(95) = a(10): a13(96) = a(11): a13(97) = a(12):
a13(105) = a(13): a13(106) = a(14): a13(107) = a(15): a13(108) = a(16): a13(109) = a(17): a13(110) = a(18):
a13(118) = a(19): a13(119) = a(20): a13(120) = a(21): a13(121) = a(22): a13(122) = a(23): a13(123) = a(24):
a13(131) = a(25): a13(132) = a(26): a13(133) = a(27): a13(134) = a(28): a13(135) = a(29): a13(136) = a(30):
a13(144) = a(31): a13(145) = a(32): a13(146) = a(33): a13(147) = a(34): a13(148) = a(35): a13(149) = a(36):
a13(157) = a(37): a13(158) = a(38): a13(159) = a(39): a13(160) = a(40): a13(161) = a(41): a13(162) = a(42):
n53 = 42: GoSub 910 'Remove used integers a() from available integers b1()
Erase b, c: GoTo 420
Else
a13(7) = a(1): a13(8) = a(7): a13(9) = a(13): a13(10) = a(19): a13(11) = a(25): a13(12) = a(31): a13(13) = a(37):
a13(20) = a(2): a13(21) = a(8): a13(22) = a(14): a13(23) = a(20): a13(24) = a(26): a13(25) = a(32): a13(26) = a(38):
a13(33) = a(3): a13(34) = a(9): a13(35) = a(15): a13(36) = a(21): a13(37) = a(27): a13(38) = a(33): a13(39) = a(39):
a13(46) = a(4): a13(47) = a(10): a13(48) = a(16): a13(49) = a(22): a13(50) = a(28): a13(51) = a(34): a13(52) = a(40):
a13(59) = a(5): a13(60) = a(11): a13(61) = a(17): a13(62) = a(23): a13(63) = a(29): a13(64) = a(35): a13(65) = a(41):
a13(72) = a(6): a13(73) = a(12): a13(74) = a(18): a13(75) = a(24): a13(76) = a(30): a13(77) = a(36): a13(78) = a(42):
GoSub 800 'Double Check Identical Integers a13()
If fl1 = 1 Then
' n9 = n9 + 1: GoSub 650 'Print Composed Squares a13()
n9 = n9 + 1: GoSub 640 'Print Composed Lines a13()
End If
Erase b1, b, c: GoTo 1000 'Print only first square
End If
5
b(c(17)) = 0: c(17) = 0
170 b(c(18)) = 0: c(18) = 0
180 b(c(19)) = 0: c(19) = 0
190 b(c(20)) = 0: c(20) = 0
200 b(c(23)) = 0: c(23) = 0
230 b(c(24)) = 0: c(24) = 0
240 b(c(25)) = 0: c(25) = 0
250 b(c(26)) = 0: c(26) = 0
260 Next j26
b(c(16)) = 0: c(16) = 0
160 b(c(21)) = 0: c(21) = 0
210 b(c(22)) = 0: c(22) = 0
220 b(c(27)) = 0: c(27) = 0
270 Next j27
b(c(11)) = 0: c(11) = 0
110 b(c(12)) = 0: c(12) = 0
120 b(c(31)) = 0: c(31) = 0
310 b(c(32)) = 0: c(32) = 0
320 Next j32
b(c(10)) = 0: c(10) = 0
100 b(c(33)) = 0: c(33) = 0
330 Next j33
b(c(5)) = 0: c(5) = 0
50 b(c(6)) = 0: c(6) = 0
60 b(c(37)) = 0: c(37) = 0
370 b(c(38)) = 0: c(38) = 0
380 Next j38
b(c(4)) = 0: c(4) = 0
40 b(c(39)) = 0: c(39) = 0
390 Next j39
b(c(8)) = 0: c(8) = 0
80 b(c(9)) = 0: c(9) = 0
90 b(c(14)) = 0: c(14) = 0
140 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(34)) = 0: c(34) = 0
340 b(c(35)) = 0: c(35) = 0
350 Next j35
b(c(7)) = 0: c(7) = 0
70 b(c(13)) = 0: c(13) = 0
130 b(c(30)) = 0: c(30) = 0
300 b(c(36)) = 0: c(36) = 0
360 Next j36
b(c(2)) = 0: c(2) = 0
20 b(c(3)) = 0: c(3) = 0
30 b(c(40)) = 0: c(40) = 0
400 b(c(41)) = 0: c(41) = 0
410 Next j41
b(c(1)) = 0: c(1) = 0
10 b(c(42)) = 0: c(42) = 0
420 Next j42
Erase b1, b, c
1000 Next j101
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Prime13c1")
End
' Print results (selected numbers)
640 For i1 = 1 To 169
Cells(n9, i1).Value = a13(i1)
Next i1
Cells(n9, 170).Value = s13
Cells(n9, 171).Value = j100
Return
' Print results (squares)
650 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 = "MC = " + CStr(s13)
i3 = 0
For i1 = 1 To 13
For i2 = 1 To 13
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a13(i3)
Next i2
Next i1
Return
' Double Check Identical Numbers a13()
800 fl1 = 1
For i1 = 1 To 169
a20 = a13(i1): If a20 = 0 Then GoTo 810
For i2 = (1 + i1) To 169
If a20 = a13(i2) Then fl1 = 0: Return
Next i2
810 Next i1
Return
' Remove used integers a() from available integers b1()
910 For i1 = 1 To n53
b1(a(i1)) = 0
Next i1
Return
End Sub