' Constructs 9 x 9 Composed Magic Squares (Distinct Integers)
' Associated Magic Corner Squares and - Rectangles
' Tested with Office 2007 under Windows 7
Sub Priem9f3()
Dim a1(81), a(81), a9(81), b1(81), b(81), c(81)
y = MsgBox("Locked", vbCritical, "Routine Priem9f3")
End
Sheets("Klad1").Select
n5 = 0: n9 = 0: k1 = 1: k2 = 1
t1 = Timer
For j101 = 1 To 81
' Pre Select j16
m11 = j101: If m11 = 41 Then GoTo 1000
' Read Pairs
Pr3 = 82: Cntr3 = 41: nvar = 81
s4 = 2 * Pr3 'MC4
s5 = 5 * Cntr3 'MC5
s9 = 9 * Cntr3 'MC9
For j1 = 1 To nvar
x = j1: a1(j1) = x: b1(x) = x
Next j1
m1 = 1: m2 = nvar
' Generate Associated Corner Square 4 x 4
b(Cntr3) = Cntr3 'Block Center Element
For j16 = m11 To m11 'a(16)
If b(a1(j16)) = 0 Then b(a1(j16)) = a1(j16): c(16) = a1(j16) Else GoTo 160
a(16) = a1(j16)
For j15 = m1 To m2 'a(15)
If b(a1(j15)) = 0 Then b(a1(j15)) = a1(j15): c(15) = a1(j15) Else GoTo 150
a(15) = a1(j15)
For j14 = m1 To m2 'a(14)
If b(a1(j14)) = 0 Then b(a1(j14)) = a1(j14): c(14) = a1(j14) Else GoTo 140
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 130
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
For j12 = m1 To m2 'a(12)
If b(a1(j12)) = 0 Then b(a1(j12)) = a1(j12): c(12) = a1(j12) Else GoTo 120
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 70
If b1(a(11)) = 0 Then GoTo 70
a(10) = s4 - a(12) - a(14) - a(16)
If a(10) < a1(m1) Or a(10) > a1(m2) Then GoTo 70
If b1(a(10)) = 0 Then GoTo 70
a(9) = s4 - a(10) - a(11) - a(12)
If a(9) < a1(m1) Or a(9) > a1(m2) Then GoTo 70
If b1(a(9)) = 0 Then GoTo 70
a(8) = 0.5 * s4 - a(9): a(7) = 0.5 * s4 - a(10): a(6) = 0.5 * s4 - a(11):
a(5) = 0.5 * s4 - a(12): a(4) = 0.5 * s4 - a(13): a(3) = 0.5 * s4 - a(14):
a(2) = 0.5 * s4 - a(15): a(1) = 0.5 * s4 - a(16):
n45 = 16: GoSub 820 'Check Identical Numbers
If fl1 = 0 Then GoTo 70
a9(1) = a(1): a9(2) = a(2): a9(3) = a(3): a9(4) = a(4):
a9(10) = a(5): a9(11) = a(6): a9(12) = a(7): a9(13) = a(8):
a9(19) = a(9): a9(20) = a(10): a9(21) = a(11): a9(22) = a(12):
a9(28) = a(13): a9(29) = a(14): a9(30) = a(15): a9(31) = a(16):
n53 = 16: GoSub 910 'Remove used integers a() from available integers b1()
Erase b, c: GoTo 200
70 b(c(12)) = 0: c(12) = 0
120 Next j12
b(c(13)) = 0: c(13) = 0
130 b(c(14)) = 0: c(14) = 0
140 Next j14
b(c(15)) = 0: c(15) = 0
150 Next j15
b(c(16)) = 0: c(16) = 0
160 Next j16
Erase b1, b, c: GoTo 1000 'Not Found, Next j101
' Determine Border Sections 4 x 5 (2 ea)
200 n10 = 0 'Continue
b(Cntr3) = Cntr3 'Block Center Element
For j20 = m1 To m2 'a(20)
If b1(a1(j20)) = 0 Then GoTo 3200
If b(a1(j20)) = 0 Then b(a1(j20)) = a1(j20): c(20) = a1(j20) Else GoTo 3200
a(20) = a1(j20)
Cells(k1 + 1, 1).Select: Cells(k1 + 1, 1).Value = j20
a(1) = 2 * s5 / 5 - a(20): If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 3010
For j19 = m1 To m2 'a(19)
If b1(a1(j19)) = 0 Then GoTo 3190
If b(a1(j19)) = 0 Then b(a1(j19)) = a1(j19): c(19) = a1(j19) Else GoTo 3190
a(19) = a1(j19)
Cells(k1 + 2, 1).Select: Cells(k1 + 2, 1).Value = j19
a(2) = 2 * s5 / 5 - a(19): If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 3020
For j18 = m1 To m2 'a(18)
If b1(a1(j18)) = 0 Then GoTo 3180
If b(a1(j18)) = 0 Then b(a1(j18)) = a1(j18): c(18) = a1(j18) Else GoTo 3180
a(18) = a1(j18)
a(3) = 2 * s5 / 5 - a(18): If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 3030
For j17 = m1 To m2 'a(17)
If b1(a1(j17)) = 0 Then GoTo 3170
If b(a1(j17)) = 0 Then b(a1(j17)) = a1(j17): c(17) = a1(j17) Else GoTo 3170
a(17) = a1(j17)
a(16) = s5 - a(17) - a(18) - a(19) - a(20)
If a(16) < a1(m1) Or a(16) > a1(m2) Then GoTo 3160
If b1(a(16)) = 0 Then GoTo 3160
If b(a(16)) = 0 Then b(a(16)) = a(16): c(16) = a(16) Else GoTo 3160
a(5) = 2 * s5 / 5 - a(16): If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 3050
a(4) = 2 * s5 / 5 - a(17): If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 3040
For j15 = m1 To m2 'a(15)
If b1(a1(j15)) = 0 Then GoTo 3150
If b(a1(j15)) = 0 Then b(a1(j15)) = a1(j15): c(15) = a1(j15) Else GoTo 3150
a(15) = a1(j15)
a(11) = -s5 + a(15) + a(17) + a(18) + a(19) + 2 * a(20)
If a(11) < a1(m1) Or a(11) > a1(m2) Then GoTo 3110
If b1(a(11)) = 0 Then GoTo 3110
If b(a(11)) = 0 Then b(a(11)) = a(11): c(11) = a(11) Else GoTo 3110
a(10) = 2 * s5 / 5 - a(11): If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 3100
a(6) = 2 * s5 / 5 - a(15): If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 3060
For j14 = m1 To m2 'a(14)
If b1(a1(j14)) = 0 Then GoTo 3140
If b(a1(j14)) = 0 Then b(a1(j14)) = a1(j14): c(14) = a1(j14) Else GoTo 3140
a(14) = a1(j14)
a(13) = 2 * s5 - 2 * a(14) - 2 * a(15) - a(18) - 2 * a(19) - 2 * a(20)
If a(13) < a1(m1) Or a(13) > a1(m2) Then GoTo 3130
If b1(a(13)) = 0 Then GoTo 3130
If b(a(13)) = 0 Then b(a(13)) = a(13): c(13) = a(13) Else GoTo 3130
a(12) = a(14) - a(17) + a(19)
If a(12) < a1(m1) Or a(12) > a1(m2) Then GoTo 3120
If b1(a(12)) = 0 Then GoTo 3120
If b(a(12)) = 0 Then b(a(12)) = a(12): c(12) = a(12) Else GoTo 3120
a(9) = 2 * s5 / 5 - a(12): If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 3090
a(8) = 2 * s5 / 5 - a(13): If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 3080
a(7) = 2 * s5 / 5 - a(14): If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 3070
If n10 = 1 Then
a9(41) = s9 - a(17) - a(13) - a(9) - a(5) - a9(9) - a9(17) - a9(25) - a9(33)
If a9(41) < a1(m1) Or a9(41) > a1(m2) Then GoTo 5:
If b1(a9(41)) = 0 Then GoTo 5
If b(a9(41)) <> 0 Then GoTo 5
End If
n10 = n10 + 1
If n10 = 1 Then
a9(5) = a(1): a9(6) = a(2): a9(7) = a(3): a9(8) = a(4): a9(9) = a(5):
a9(14) = a(6): a9(15) = a(7): a9(16) = a(8): a9(17) = a(9): a9(18) = a(10):
a9(23) = a(11): a9(24) = a(12): a9(25) = a(13): a9(26) = a(14): a9(27) = a(15):
a9(32) = a(16): a9(33) = a(17): a9(34) = a(18): a9(35) = a(19): a9(36) = a(20):
n53 = 20: GoSub 910 'Remove used integers a() from available integers b1()
Erase b, c: GoTo 3200
Else
a9(37) = a(1): a9(38) = a(6): a9(39) = a(11): a9(40) = a(16):
a9(46) = a(2): a9(47) = a(7): a9(48) = a(12): a9(49) = a(17):
a9(55) = a(3): a9(56) = a(8): a9(57) = a(13): a9(58) = a(18):
a9(64) = a(4): a9(65) = a(9): a9(66) = a(14): a9(67) = a(19):
a9(73) = a(5): a9(74) = a(10): a9(75) = a(15): a9(76) = a(20):
n53 = 20: GoSub 910 'Remove used integers a() from available integers b1()
Erase b, c: GoTo 2000
End If
5
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(12)) = 0: c(12) = 0
3120 b(c(13)) = 0: c(13) = 0
3130 b(c(14)) = 0: c(14) = 0
3140 Next j14
b(c(6)) = 0: c(6) = 0
3060 b(c(10)) = 0: c(10) = 0
3100 b(c(11)) = 0: c(11) = 0
3110 b(c(15)) = 0: c(15) = 0
3150 Next j15
b(c(4)) = 0: c(4) = 0
3040 b(c(5)) = 0: c(5) = 0
3050 b(c(16)) = 0: c(16) = 0
3160 b(c(17)) = 0: c(17) = 0
3170 Next j17
b(c(3)) = 0: c(3) = 0
3030 b(c(18)) = 0: c(18) = 0
3180 Next j18
b(c(2)) = 0: c(2) = 0
3020 b(c(19)) = 0: c(19) = 0
3190 Next j19
b(c(1)) = 0: c(1) = 0
3010 b(c(20)) = 0: c(20) = 0
3200 Next j20
Erase b1, b, c: GoTo 1000 'Not Found, Next j101
' Generate Associated Magic Square 5 x 5
2000 'Continue
a(25) = a9(41): b(a(25)) = a(25)
a(13) = 0.2 * s5: b(a(13)) = a(13)
a(1) = 0.4 * s5 - a(25)
If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 2010
If b1(a(1)) = 0 Then GoTo 2010
If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 2010
For j24 = m1 To m2 'a(24)
If b1(a1(j24)) = 0 Then GoTo 2240
If b(a1(j24)) = 0 Then b(a1(j24)) = a1(j24): c(24) = a1(j24) Else GoTo 2240
a(24) = a1(j24)
a(2) = 0.4 * s5 - a(24)
If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 2020
If b1(a(2)) = 0 Then GoTo 2020
If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 2020
For j23 = m1 To m2 'a(23)
If b1(a1(j23)) = 0 Then GoTo 2230
If b(a1(j23)) = 0 Then b(a1(j23)) = a1(j23): c(23) = a1(j23) Else GoTo 2230
a(23) = a1(j23)
a(3) = 0.4 * s5 - a(23)
If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 2030
If b1(a(3)) = 0 Then GoTo 2030
If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 2030
For j22 = m1 To m2 'a(22)
If b1(a1(j22)) = 0 Then GoTo 2220
If b(a1(j22)) = 0 Then b(a1(j22)) = a1(j22): c(22) = a1(j22) Else GoTo 2220
a(22) = a1(j22)
a(21) = s5 - a(22) - a(23) - a(24) - a(25)
If a(21) < a1(m1) Or a(21) > a1(m2) Then GoTo 2210
If b1(a(21)) = 0 Then GoTo 2210
If b(a(21)) = 0 Then b(a(21)) = a(21): c(21) = a(21) Else GoTo 2210
a(5) = 0.4 * s5 - a(21)
If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 2050
If b1(a(5)) = 0 Then GoTo 2050
If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 2050
a(4) = 0.4 * s5 - a(22)
If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 2040
If b1(a(4)) = 0 Then GoTo 2040
If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 2040
For j20 = m1 To m2 'a(20)
If b1(a1(j20)) = 0 Then GoTo 2200
If b(a1(j20)) = 0 Then b(a1(j20)) = a1(j20): c(20) = a1(j20) Else GoTo 2200
a(20) = a1(j20)
a(6) = 2 * s5 / 5 - a(20)
If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 2060
If b1(a(6)) = 0 Then GoTo 2060
If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 2060
For j19 = m1 To m2 'a(19)
If b1(a1(j19)) = 0 Then GoTo 2190
If b(a1(j19)) = 0 Then b(a1(j19)) = a1(j19): c(19) = a1(j19) Else GoTo 2190
a(19) = a1(j19)
a(7) = 0.4 * s5 - a(19)
If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 2070
If b1(a(7)) = 0 Then GoTo 2070
If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 2070
For j18 = m1 To m2 'a(18)
If b1(a1(j18)) = 0 Then GoTo 2180
If b(a1(j18)) = 0 Then b(a1(j18)) = a1(j18): c(18) = a1(j18) Else GoTo 2180
a(18) = a1(j18)
a(8) = 0.4 * s5 - a(18)
If a(8) < a1(m1) Or a(8) > a1(m2) Then GoTo 2080
If b1(a(8)) = 0 Then GoTo 2080
If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 2080
For j17 = m1 To m2 'a(17)
If b1(a1(j17)) = 0 Then GoTo 2170
If b(a1(j17)) = 0 Then b(a1(j17)) = a1(j17): c(17) = a1(j17) Else GoTo 2170
a(17) = a1(j17)
a(16) = s5 - a(17) - a(18) - a(19) - a(20)
If a(16) < a1(m1) Or a(16) > a1(m2) Then GoTo 2160
If b1(a(16)) = 0 Then GoTo 2160
If b(a(16)) = 0 Then b(a(16)) = a(16): c(16) = a(16) Else GoTo 2160
a(15) = s5 / 5 + a(16) - a(20) + a(21) - a(25)
If a(15) < a1(m1) Or a(15) > a1(m2) Then GoTo 2150
If b1(a(15)) = 0 Then GoTo 2150
If b(a(15)) = 0 Then b(a(15)) = a(15): c(15) = a(15) Else GoTo 2150
a(14) = s5 / 5 + a(17) - a(19) + a(22) - a(24)
If a(14) < a1(m1) Or a(14) > a1(m2) Then GoTo 2140
If b1(a(14)) = 0 Then GoTo 2140
If b(a(14)) = 0 Then b(a(14)) = a(14): c(14) = a(14) Else GoTo 2140
a(12) = 0.4 * s5 - a(14)
If a(12) < a1(m1) Or a(12) > a1(m2) Then GoTo 2120
If b1(a(12)) = 0 Then GoTo 2120
If b(a(12)) = 0 Then b(a(12)) = a(12): c(12) = a(12) Else GoTo 2120
a(11) = 0.4 * s5 - a(15)
If a(11) < a1(m1) Or a(11) > a1(m2) Then GoTo 2110:
If b1(a(11)) = 0 Then GoTo 2110
If b(a(11)) = 0 Then b(a(11)) = a(11): c(11) = a(11) Else GoTo 2110
a(10) = 2 * s5 / 5 - a(16)
If a(10) < a1(m1) Or a(10) > a1(m2) Then GoTo 2100:
If b1(a(10)) = 0 Then GoTo 2100
If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 2100
a(9) = 0.4 * s5 - a(17)
If a(9) < a1(m1) Or a(9) > a1(m2) Then GoTo 2090
If b1(a(9)) = 0 Then GoTo 2090
If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 2090
n45 = 25: GoSub 820
If fl1 = 0 Then GoTo 2005
' Assign a() to a9()
a9(42) = a(24): a9(43) = a(23): a9(44) = a(22): a9(45) = a(21):
a9(50) = a(20): a9(51) = a(19): a9(52) = a(18): a9(53) = a(17): a9(54) = a(16):
a9(59) = a(15): a9(60) = a(14): a9(61) = a(13): a9(62) = a(12): a9(63) = a(11):
a9(68) = a(10): a9(69) = a(9): a9(70) = a(8): a9(71) = a(7): a9(72) = a(6):
a9(77) = a(5): a9(78) = a(4): a9(79) = a(3): a9(80) = a(2): a9(81) = a(1):
GoSub 800 'Double Check Identical Integers a9()
If fl1 = 1 Then
n9 = n9 + 1: GoSub 650 'Print Composed Squares a9()
End If
Erase b1, b, c: GoTo 1000 'Print only first square
2005
b(c(9)) = 0: c(9) = 0
2090 b(c(10)) = 0: c(10) = 0
2100 b(c(11)) = 0: c(11) = 0
2110 b(c(12)) = 0: c(12) = 0
2120 b(c(14)) = 0: c(14) = 0
2140 b(c(15)) = 0: c(15) = 0
2150 b(c(16)) = 0: c(16) = 0
2160 b(c(17)) = 0: c(17) = 0
2170 Next j17
b(c(8)) = 0: c(8) = 0
2080 b(c(18)) = 0: c(18) = 0
2180 Next j18
b(c(7)) = 0: c(7) = 0
2070 b(c(19)) = 0: c(19) = 0
2190 Next j19
b(c(6)) = 0: c(6) = 0
2060 b(c(20)) = 0: c(20) = 0
2200 Next j20
b(c(4)) = 0: c(4) = 0
2040 b(c(5)) = 0: c(5) = 0
2050 b(c(21)) = 0: c(21) = 0
2210 b(c(22)) = 0: c(22) = 0
2220 Next j22
b(c(3)) = 0: c(3) = 0
2030 b(c(23)) = 0: c(23) = 0
2230 Next j23
b(c(2)) = 0: c(2) = 0
2020 b(c(24)) = 0: c(24) = 0
2240 Next j24
b(c(1)) = 0: c(1) = 0
2010 b(c(25)) = 0: c(25) = 0
2250
Erase b1, b, c
1000 Next j101
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Priem9f3")
End
' Print results (squares)
650 n5 = n5 + 1
If n5 = 3 Then
n5 = 1: k1 = k1 + 10: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 10
End If
Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = "MC = " + CStr(s9)
i3 = 0
For i1 = 1 To 9
For i2 = 1 To 9
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a9(i3)
Next i2
Next i1
Return
' Double Check Identical Numbers a9()
800 fl1 = 1
For i1 = 1 To 81
a20 = a9(i1): If a20 = 0 Then GoTo 810
For i2 = (1 + i1) To 81
If a20 = a9(i2) Then fl1 = 0: Return
Next i2
810 Next i1
Return
' Double Check Identical Numbers a()
820 fl1 = 1
For i1 = 1 To n45
a20 = a(i1)
For i2 = (1 + i1) To n45
If a20 = a(i2) Then fl1 = 0: Return
Next i2
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