' Constructs 13 x 13 Composed Magic Squares for Natural Numbers (Part 2)
' Associated Magic Corner Squares and - Rectangles
' Tested with Office 365 under Windows 10
Sub Prime13c2()
Dim a1(169), a(169), a7(49), a13(169), b1(169), b(169), c(169)
y = MsgBox("Locked", vbCritical, "Routine Prime13c2")
End
Sheets("Klad1").Select
n5 = 0: n9 = 0: k1 = 1: k2 = 1
ShtNm2 = "Lines13"
Pr3 = 170: Cntr3 = 85 'Center Element
s3 = 3 * Cntr3 'MC3
s4 = 2 * Pr3 'MC4
s6 = 6 * Cntr3 'MC6
s7 = 7 * Cntr3 'MC7
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
' Read Partial Completed Square 13 x 13
For i1 = 1 To 169
a(i1) = Sheets(ShtNm2).Cells(j101, i1).Value
Next i1
n10 = 0: n53 = 169: GoSub 910 'Remove used pairs from b1()
Erase a13
For i1 = 1 To 169
a13(i1) = a(i1)
Next i1
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 Associated Magic Square 4 x 4
a(16) = a13(85): b(a(16)) = a(16)
b(Cntr3) = Cntr3
For j15 = m1 To m2 'a(15)
If b1(a1(j15)) = 0 Then GoTo 2150
If b(a1(j15)) = 0 Then b(a1(j15)) = a1(j15): c(15) = a1(j15) Else GoTo 2150
a(15) = a1(j15)
For j14 = m1 To m2 'a(14)
If b1(a1(j14)) = 0 Then GoTo 2140
If b(a1(j14)) = 0 Then b(a1(j14)) = a1(j14): c(14) = a1(j14) Else GoTo 2140
a(14) = a1(j14)
a(13) = s4 - a(14) - a(15) - a(16)
If a(13) < a1(m1) Or a(13) > a1(m2) Then GoTo 2130
If b1(a(13)) = 0 Then GoTo 2130
If b(a(13)) = 0 Then b(a(13)) = a(13): c(13) = a(13) Else GoTo 2130
For j12 = m1 To m2 'a(12)
If b1(a1(j12)) = 0 Then GoTo 2120
If b(a1(j12)) = 0 Then b(a1(j12)) = a1(j12): c(12) = a1(j12) Else GoTo 2120
a(12) = a1(j12)
a(11) = s4 - a(12) - a(15) - a(16)
If a(11) < a1(m1) Or a(11) > a1(m2) Then GoTo 2070
If b1(a(11)) = 0 Then GoTo 2070
a(10) = s4 - a(12) - a(14) - a(16)
If a(10) < a1(m1) Or a(10) > a1(m2) Then GoTo 2070
If b1(a(10)) = 0 Then GoTo 2070
a(9) = s4 - a(10) - a(11) - a(12)
If a(9) < a1(m1) Or a(9) > a1(m2) Then GoTo 2070
If b1(a(9)) = 0 Then GoTo 2070
a(8) = Pr3 - a(9)
If a(8) < a1(m1) Or a(8) > a1(m2) Then GoTo 2070
If b1(a(8)) = 0 Then GoTo 2070
a(7) = Pr3 - a(10)
If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 2070:
If b1(a(7)) = 0 Then GoTo 2070
a(6) = Pr3 - a(11)
If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 2070:
If b1(a(6)) = 0 Then GoTo 2070
a(5) = Pr3 - a(12)
If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 2070:
If b1(a(5)) = 0 Then GoTo 2070
a(4) = Pr3 - a(13)
If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 2070:
If b1(a(4)) = 0 Then GoTo 2070
a(3) = Pr3 - a(14)
If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 2070:
If b1(a(3)) = 0 Then GoTo 2070
a(2) = Pr3 - a(15)
If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 2070:
If b1(a(2)) = 0 Then GoTo 2070
a(1) = Pr3 - a(16)
If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 2070:
If b1(a(1)) = 0 Then GoTo 2070
n43 = 16: GoSub 820
If fl1 = 0 Then GoTo 2070
' Assign a() to a13()
a13(86) = a(15): a13(87) = a(14): a13(88) = a(13):
a13(98) = a(12): a13(99) = a(11): a13(100) = a(10): a13(101) = a(9):
a13(111) = a(8): a13(112) = a(7): a13(113) = a(6): a13(114) = a(5):
a13(124) = a(4): a13(125) = a(3): a13(126) = a(2): a13(127) = a(1):
n53 = 16: GoSub 910 'Remove used pairs from b1()
Erase b, c: GoTo 200
2070 b(c(12)) = 0: c(12) = 0
2120 Next j12
b(c(13)) = 0: c(13) = 0
2130 b(c(14)) = 0: c(14) = 0
2140 Next j14
b(c(15)) = 0: c(15) = 0
2150 Next j15
Erase b1, b, c: GoTo 1000 'Not Found, Next j101
' Generate Magic Square 3 x 3
200 b(Cntr3) = Cntr3 'Continue
For j9 = m1 To m2 'a(9)
If b(a1(j9)) = 0 Then b(a1(j9)) = a1(j9): c(9) = a1(j9) Else GoTo 260
a(9) = a1(j9)
For j8 = m1 To m2 'a(8)
If b(a1(j8)) = 0 Then b(a1(j8)) = a1(j8): c(8) = a1(j8) Else GoTo 220
a(8) = a1(j8)
a(7) = s3 - a(8) - a(9):
If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 210:
If b1(a(7)) = 0 Then GoTo 210
a(6) = 4 * s3 / 3 - a(8) - 2 * a(9):
If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 210:
If b1(a(6)) = 0 Then GoTo 210
a(5) = s3 / 3:
If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 210:
If b1(a(5)) = 0 Then GoTo 210
a(4) = -2 * s3 / 3 + a(8) + 2 * a(9):
If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 210:
If b1(a(4)) = 0 Then GoTo 210
a(3) = -s3 / 3 + a(8) + a(9):
If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 210:
If b1(a(3)) = 0 Then GoTo 210
a(2) = 2 * s3 / 3 - a(8):
If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 210:
If b1(a(2)) = 0 Then GoTo 210
a(1) = 2 * s3 / 3 - a(9):
If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 210:
If b1(a(1)) = 0 Then GoTo 210
' Exclude solutions with identical numbers a()
n43 = 9: GoSub 820: If fl1 = 0 Then GoTo 210
' Assign to a13()
a13(141) = a(1): a13(142) = a(2): a13(143) = a(3):
a13(154) = a(4): a13(155) = a(5): a13(156) = a(6):
a13(167) = a(7): a13(168) = a(8): a13(169) = a(9):
n53 = 9: GoSub 910 'Remove used pairs from b1()
Erase b, c: GoTo 2000
210 b(c(8)) = 0: c(8) = 0
220 Next j8
b(c(9)) = 0: c(9) = 0
260 Next j9
Erase b1, b, c: GoTo 1000 'Not Found, Next j101
2000 'Continue
' Generate Associated Magic Rectangles 3 x 4
For j12 = m1 To m2 'a(12)
If b1(a1(j12)) = 0 Then GoTo 3120
If b(a1(j12)) = 0 Then b(a1(j12)) = a1(j12): c(12) = a1(j12) Else GoTo 3120
a(12) = a1(j12)
a(1) = Pr3 - a(12): If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 3010
For j11 = m1 To m2 'a(11)
If b1(a1(j11)) = 0 Then GoTo 3110
If b(a1(j11)) = 0 Then b(a1(j11)) = a1(j11): c(11) = a1(j11) Else GoTo 3110
a(11) = a1(j11)
a(2) = Pr3 - a(11): If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 3020
For j10 = m1 To m2 'a(10)
If b1(a1(j10)) = 0 Then GoTo 3100
If b(a1(j10)) = 0 Then b(a1(j10)) = a1(j10): c(10) = a1(j10) Else GoTo 3100
a(10) = a1(j10)
a(9) = 2 * Pr3 - a(10) - a(11) - a(12)
If a(9) < a1(m1) Or a(9) > a1(m2) Then GoTo 3090:
If b1(a(9)) = 0 Then GoTo 3090
If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 3090
a(8) = 5 * Pr3 / 2 - a(10) - a(11) - 2 * a(12)
If a(8) < a1(m1) Or a(8) > a1(m2) Then GoTo 3080:
If b1(a(8)) = 0 Then GoTo 3080
If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 3080
a(7) = 3 * Pr3 - a(8) - 2 * a(11) - 2 * a(12)
If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 3070:
If b1(a(7)) = 0 Then GoTo 3070
If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 3070
a(6) = Pr3 - a(7): If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 3060
a(5) = Pr3 - a(8): If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 3050
a(4) = Pr3 - a(9): If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 3040
a(3) = Pr3 - a(10): If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 3030
n10 = n10 + 1
Select Case n10
Case 1:
a13(137) = a(1): a13(138) = a(2): a13(139) = a(3): a13(140) = a(4):
a13(150) = a(5): a13(151) = a(6): a13(152) = a(7): a13(153) = a(8):
a13(163) = a(9): a13(164) = a(10): a13(165) = a(11): a13(166) = a(12):
n53 = 12: GoSub 910 'Remove used pairs from b1()
Erase b, c: GoTo 3120
Case 2:
a13(89) = a(1): a13(90) = a(5): a13(91) = a(9):
a13(102) = a(2): a13(103) = a(6): a13(104) = a(10):
a13(115) = a(3): a13(116) = a(7): a13(117) = a(11):
a13(128) = a(4): a13(129) = a(8): a13(130) = a(12):
GoSub 800 'Double Check Identical Integers a13()
If fl1 = 1 Then
n9 = n9 + 1: GoSub 650 'Print results (squares)
End If
Erase b1, b, c: GoTo 1000 'Print only first square
End Select
b(c(3)) = 0: c(3) = 0
3030 b(c(4)) = 0: c(4) = 0
3040 b(c(5)) = 0: c(5) = 0
3050 b(c(6)) = 0: c(6) = 0
3060 b(c(7)) = 0: c(7) = 0
3070 b(c(8)) = 0: c(8) = 0
3080 b(c(9)) = 0: c(9) = 0
3090 b(c(10)) = 0: c(10) = 0
3100 Next j10
b(c(2)) = 0: c(2) = 0
3020 b(c(11)) = 0: c(11) = 0
3110 Next j11
b(c(1)) = 0: c(1) = 0
3010 b(c(12)) = 0: c(12) = 0
3120 Next j12
1000 Erase b1, b, c
Next j101
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Prime13c2")
End
' 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
' Double Check Identical Numbers a()
820 fl1 = 1
For i1 = 1 To n43
a20 = a(i1): If a20 = 0 Then GoTo 825
For i2 = (1 + i1) To n43
If a20 = a(i2) Then fl1 = 0: Return
Next i2
825 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