' Constructs 13 x 13 Composed Magic Squares for Natural Numbers (Part 2)
' Overlapping Sub Squares Order 7 and 3
' Tested with Office 2007 under Windows 7
Sub Prime13d2()
Dim a1(169), a(169), a7(49), a13(169), b1(169), b(169), c(169)
y = MsgBox("Locked", vbCritical, "Routine Prime13d2")
End
Sheets("Klad1").Select
n5 = 0: n9 = 0: k1 = 1: k2 = 1
ShtNm2 = "Lines13b"
Cntr3 = 85: Pr3 = 2 * Cntr3:
s3 = 3 * Cntr3 'MC3
s4 = 2 * Pr3 'MC4
s7 = 7 * Cntr3 'MC7
s13 = 13 * Cntr3 'MC13
t1 = Timer
For j101 = 2 To 81
' Redefine Varaibles
nVar1 = 169
For i1 = 1 To nVar1
a1(i1) = i1
Next i1
m1 = 1: m2 = nVar1
pMax = a1(m2)
Erase b1
For i1 = m1 To m2: b1(a1(i1)) = a1(i1): Next i1
' 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 Squares 4 x 4
b(Cntr3) = Cntr3 'Block Center Square
n10 = 0
For j16 = m1 To m2 'a(16)
If b1(a1(j16)) = 0 Then GoTo 2160
If b(a1(j16)) = 0 Then b(a1(j16)) = a1(j16): c(16) = a1(j16) Else GoTo 2160
a(16) = a1(j16)
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): a(7) = Pr3 - a(10): a(6) = Pr3 - a(11): a(5) = Pr3 - a(12):
a(4) = Pr3 - a(13): a(3) = Pr3 - a(14): a(2) = Pr3 - a(15): a(1) = Pr3 - a(16):
n43 = 16: GoSub 820
If fl1 = 0 Then GoTo 2070
n10 = n10 + 1
Select Case n10
Case 1
a13(118) = a(1): a13(119) = a(2): a13(120) = a(3): a13(121) = a(4):
a13(131) = a(5): a13(132) = a(6): a13(133) = a(7): a13(134) = a(8):
a13(144) = a(9): a13(145) = a(10): a13(146) = a(11): a13(147) = a(12):
a13(157) = a(13): a13(158) = a(14): a13(159) = a(15): a13(160) = a(16):
n53 = 16: GoSub 910 'Remove used pairs from b1()
Erase b, c: GoTo 2160 'Find Square 2
Case 2
a13(10) = a(1): a13(11) = a(2): a13(12) = a(3): a13(13) = a(4):
a13(23) = a(5): a13(24) = a(6): a13(25) = a(7): a13(26) = a(8):
a13(36) = a(9): a13(37) = a(10): a13(38) = a(11): a13(39) = a(12):
a13(49) = a(13): a13(50) = a(14): a13(51) = a(15): a13(52) = a(16):
n53 = 16: GoSub 910 'Remove used pairs from b1()
Erase b, c: GoTo 200 'Continue
End Select
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
b(c(16)) = 0: c(16) = 0
2160 Next j16
GoTo 1000 'Not Found
200 'Continue
' Generate Magic Corner Square 3 x 3
n10 = 0
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 + 1 * 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 + 1 * a(8) + 1 * 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
n10 = n10 + 1
Select Case n10
Case 1
a13(83) = a(6): a13(84) = a(4): a13(85) = a(5):
a13(96) = a(9): a13(97) = a(7): a13(98) = a(8):
a13(109) = a(3): a13(110) = a(1): a13(111) = a(2):
n53 = 9: GoSub 910 'Remove used pairs from b1()
b1(Cntr3) = Cntr3 'Restore Center Element
Erase b, c: GoTo 260 'Find Square 2
Case 2
a13(59) = a(2): a13(60) = a(1): a13(61) = a(3):
a13(72) = a(8): a13(73) = a(7): a13(74) = a(9):
a13(85) = a(5): a13(86) = a(4): a13(87) = a(6):
n53 = 9: GoSub 910 'Remove used pairs from b1()
Erase b, c: GoTo 3000 'Continue
End Select
210 b(c(8)) = 0: c(8) = 0
220 Next j8
b(c(9)) = 0: c(9) = 0
260 Next j9
GoTo 1000 'Not Found
3000 'Continue
' Generate Associated Magic Rectangles 3 x 4
n10 = 0
Erase a: a(25) = a13(121)
For j24 = m1 To m2 'a(24)
If b1(a1(j24)) = 0 Then GoTo 3240
If b(a1(j24)) = 0 Then b(a1(j24)) = a1(j24): c(24) = a1(j24) Else GoTo 3240
a(24) = a1(j24)
a(13) = Pr3 - a(24): If b(a(13)) = 0 Then b(a(13)) = a(13): c(13) = a(13) Else GoTo 3130
For j23 = m1 To m2 'a(23)
If b1(a1(j23)) = 0 Then GoTo 3230
If b(a1(j23)) = 0 Then b(a1(j23)) = a1(j23): c(23) = a1(j23) Else GoTo 3230
a(23) = a1(j23)
a(22) = 3 * s7 / 7 - a(23) - a(24)
If a(22) < a1(m1) Or a(22) > a1(m2) Then GoTo 3220
If b1(a(22)) = 0 Then GoTo 3220
If b(a(22)) = 0 Then b(a(22)) = a(22): c(22) = a(22) Else GoTo 3220
a(15) = Pr3 - a(22): If b(a(15)) = 0 Then b(a(15)) = a(15): c(15) = a(15) Else GoTo 3150
a(14) = Pr3 - a(23): If b(a(14)) = 0 Then b(a(14)) = a(14): c(14) = a(14) Else GoTo 3140
For j21 = m1 To m2 'a(21)
If b1(a1(j21)) = 0 Then GoTo 3210
If b(a1(j21)) = 0 Then b(a1(j21)) = a1(j21): c(21) = a1(j21) Else GoTo 3210
a(21) = a1(j21)
a(20) = 6 * s7 / 7 - 2 * a(21) - a(23) - 2 * a(24)
If a(20) < a1(m1) Or a(20) > a1(m2) Then GoTo 3200
If b1(a(20)) = 0 Then GoTo 3200
If b(a(20)) = 0 Then b(a(20)) = a(20): c(20) = a(20) Else GoTo 3200
a(19) = -3 * s7 / 7 + a(21) + a(23) + 2 * a(24)
If a(19) < a1(m1) Or a(19) > a1(m2) Then GoTo 3190
If b1(a(19)) = 0 Then GoTo 3190
If b(a(19)) = 0 Then b(a(19)) = a(19): c(19) = a(19) Else GoTo 3190
a(18) = Pr3 - a(19): If b(a(18)) = 0 Then b(a(18)) = a(18): c(18) = a(18) Else GoTo 3180
a(17) = Pr3 - a(20): If b(a(17)) = 0 Then b(a(17)) = a(17): c(17) = a(17) Else GoTo 3170
a(16) = Pr3 - a(21): If b(a(16)) = 0 Then b(a(16)) = a(16): c(16) = a(16) Else GoTo 3160
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(10) = 4 * s7 / 7 + 2 * a(11) - a(12) - 3 * a(21) - a(23) - a(24) + a(25)
If a(10) < a1(m1) Or a(10) > a1(m2) Then GoTo 3100
If b1(a(10)) = 0 Then GoTo 3100
If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 3100
a(9) = -3 * a(11) + 3 * a(21) + a(23) + a(24) - a(25)
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) = s7 / 7 - 3 * a(11) - a(12) + 3 * a(21) + a(23) + a(24) - a(25)
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) = 5 * s7 / 7 + a(11) - a(12) - 3 * a(21) - a(23) - a(24) + a(25)
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
a(2) = Pr3 - a(11): If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 3020
n10 = n10 + 1
Select Case n10
Case 1
a13(79) = a(1): a13(80) = a(2): a13(81) = a(3): a13(82) = a(4):
a13(92) = a(5): a13(93) = a(6): a13(94) = a(7): a13(95) = a(8):
a13(105) = a(9): a13(106) = a(10): a13(107) = a(11): a13(108) = a(12):
a13(122) = a(13): a13(123) = a(14): a13(124) = a(15):
a13(135) = a(16): a13(136) = a(17): a13(137) = a(18):
a13(148) = a(19): a13(149) = a(20): a13(150) = a(21):
a13(161) = a(22): a13(162) = a(23): a13(163) = a(24):
n53 = 25: GoSub 910 'Remove used pairs from b1()
a(25) = a13(49)
Erase b, c: GoTo 3240 'Find Square 2
Case 2
a13(7) = a(24): a13(8) = a(23): a13(9) = a(22):
a13(20) = a(21): a13(21) = a(20): a13(22) = a(19):
a13(33) = a(18): a13(34) = a(17): a13(35) = a(16):
a13(46) = a(15): a13(47) = a(14): a13(48) = a(13):
a13(62) = a(12): a13(63) = a(11): a13(64) = a(10): a13(65) = a(9):
a13(75) = a(8): a13(76) = a(7): a13(77) = a(6): a13(78) = a(5):
a13(88) = a(4): a13(89) = a(3): a13(90) = a(2): a13(91) = a(1):
GoSub 800 'Double Check Identical Integers a13()
If fl1 = 1 Then
n9 = n9 + 1: GoSub 650 'Print Composed Squares a13()
End If
Erase b1, b, c: GoTo 1000 'Print only first square
End Select
b(c(2)) = 0: c(2) = 0
3020 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 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
b(c(16)) = 0: c(16) = 0
3160 b(c(17)) = 0: c(17) = 0
3170 b(c(18)) = 0: c(18) = 0
3180 b(c(19)) = 0: c(19) = 0
3190 b(c(20)) = 0: c(20) = 0
3200 b(c(21)) = 0: c(21) = 0
3210 Next j21
b(c(14)) = 0: c(14) = 0
3140 b(c(15)) = 0: c(15) = 0
3150 b(c(22)) = 0: c(22) = 0
3220 b(c(23)) = 0: c(23) = 0
3230 Next j23
b(c(13)) = 0: c(13) = 0
3130 b(c(24)) = 0: c(24) = 0
3240 Next j24
1000 Erase b1, b, c
Next j101
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Prime13d2")
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 = j101
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