' Generates Medjig Squares of order 6, Magic Sum 9, Integers 0, 1, 2 and 3
' Tested with Office 2007 under Windows 7
Sub MgcSqr6d()
Dim a(36), b1(36), c1(36), b(9, 4), c(9, 4), s(16)
' Enable applicable 3 x 3 square for corresponding 6 x 6 Magic Squares
b1(1) = 8: b1(2) = 1: b1(3) = 6: b1(4) = 3: b1(5) = 5: b1(6) = 7: b1(7) = 4: b1(8) = 9: b1(9) = 2
'b1(1) = 2: b1(2) = 7: b1(3) = 6: b1(4) = 9: b1(5) = 5: b1(6) = 1: b1(7) = 4: b1(8) = 3: b1(9) = 8
'b1(1) = 2: b1(2) = 9: b1(3) = 4: b1(4) = 7: b1(5) = 5: b1(6) = 3: b1(7) = 6: b1(8) = 1: b1(9) = 8
'b1(1) = 4: b1(2) = 3: b1(3) = 8: b1(4) = 9: b1(5) = 5: b1(6) = 1: b1(7) = 2: b1(8) = 7: b1(9) = 6
'b1(1) = 4: b1(2) = 9: b1(3) = 2: b1(4) = 3: b1(5) = 5: b1(6) = 7: b1(7) = 8: b1(8) = 1: b1(9) = 6
'b1(1) = 6: b1(2) = 1: b1(3) = 8: b1(4) = 7: b1(5) = 5: b1(6) = 3: b1(7) = 2: b1(8) = 9: b1(9) = 4
'b1(1) = 6: b1(2) = 7: b1(3) = 2: b1(4) = 1: b1(5) = 5: b1(6) = 9: b1(7) = 8: b1(8) = 3: b1(9) = 4
'b1(1) = 8: b1(2) = 3: b1(3) = 4: b1(4) = 1: b1(5) = 5: b1(6) = 9: b1(7) = 6: b1(8) = 7: b1(9) = 2
y = MsgBox("Locked", vbCritical, "Routine MgcSqr6d")
End
k1 = 1: k2 = 1: n2 = 0: n3 = 0: n9 = 0
' Generate data
Sheets("Results1").Select
t1 = Timer
For j36 = 1 To 4 'a(36)
If b(9, j36) = 0 Then b(9, j36) = j36: c(9, 4) = j36 Else GoTo 360
a(36) = j36 - 1
For j35 = 1 To 4 'a(35)
If b(9, j35) = 0 Then b(9, j35) = j35: c(9, 3) = j35 Else GoTo 350
a(35) = j35 - 1
For j34 = 1 To 4 'a(34)
If b(8, j34) = 0 Then b(8, j34) = j34: c(8, 4) = j34 Else GoTo 340
a(34) = j34 - 1
For j33 = 1 To 4 'a(33)
If b(8, j33) = 0 Then b(8, j33) = j33: c(8, 3) = j33 Else GoTo 330
a(33) = j33 - 1
For j32 = 1 To 4 'a(32)
If b(7, j32) = 0 Then b(7, j32) = j32: c(7, 4) = j32 Else GoTo 320
a(32) = j32 - 1
a(31) = 9 - a(32) - a(33) - a(34) - a(35) - a(36)
j31 = a(31) + 1
If j31 <= 0 Or j31 > 4 Then GoTo 310
If b(7, j31) = 0 Then b(7, j31) = j31: c(7, 3) = j31 Else GoTo 310
For j30 = 1 To 4 'a(30)
If b(9, j30) = 0 Then b(9, j30) = j30: c(9, 2) = j30 Else GoTo 300
a(30) = j30 - 1
a(29) = 6 - a(30) - a(35) - a(36)
j29 = a(29) + 1
If j29 <= 0 Or j29 > 4 Then GoTo 290
If b(9, j29) = 0 Then b(9, j29) = j29: c(9, 1) = j29 Else GoTo 290
For j28 = 1 To 4 'a(28)
If b(8, j28) = 0 Then b(8, j28) = j28: c(8, 2) = j28 Else GoTo 280
a(28) = j28 - 1
a(27) = 6 - a(28) - a(33) - a(34)
j27 = a(27) + 1
If j27 <= 0 Or j27 > 4 Then GoTo 270
If b(8, j27) = 0 Then b(8, j27) = j27: c(8, 1) = j27 Else GoTo 270
For j26 = 1 To 4 'a(26)
If b(7, j26) = 0 Then b(7, j26) = j26: c(7, 2) = j26 Else GoTo 260
a(26) = j26 - 1
a(25) = 6 - a(26) - a(31) - a(32)
j25 = a(25) + 1
If j25 <= 0 Or j25 > 4 Then GoTo 250
If b(7, j25) = 0 Then b(7, j25) = j25: c(7, 1) = j25 Else GoTo 250
For j24 = 1 To 4 'a(24)
If b(6, j24) = 0 Then b(6, j24) = j24: c(6, 4) = j24 Else GoTo 240
a(24) = j24 - 1
For j23 = 1 To 4 'a(23)
If b(6, j23) = 0 Then b(6, j23) = j23: c(6, 3) = j23 Else GoTo 230
a(23) = j23 - 1
For j22 = 1 To 4 'a(22)
If b(5, j22) = 0 Then b(5, j22) = j22: c(5, 4) = j22 Else GoTo 220
a(22) = j22 - 1
For j21 = 1 To 4 'a(21)
If b(5, j21) = 0 Then b(5, j21) = j21: c(5, 3) = j21 Else GoTo 210
a(21) = j21 - 1
For j20 = 1 To 4 'a(20)
If b(4, j20) = 0 Then b(4, j20) = j20: c(4, 4) = j20 Else GoTo 200
a(20) = j20 - 1
a(19) = 9 - a(20) - a(21) - a(22) - a(23) - a(24)
j19 = a(19) + 1
If j19 <= 0 Or j19 > 4 Then GoTo 190
If b(4, j19) = 0 Then b(4, j19) = j19: c(4, 3) = j19 Else GoTo 190
For j18 = 1 To 4 'a(18)
If b(6, j18) = 0 Then b(6, j18) = j18: c(6, 2) = j18 Else GoTo 180
a(18) = j18 - 1
a(17) = 6 - a(18) - a(23) - a(24)
j17 = a(17) + 1
If b(6, j17) = 0 Then b(6, j17) = j17: c(6, 1) = j17 Else GoTo 170
For j16 = 1 To 4 'a(16)
If b(5, j16) = 0 Then b(5, j16) = j16: c(5, 2) = j16 Else GoTo 160
a(16) = j16 - 1
a(15) = 6 - a(16) - a(21) - a(22)
j15 = a(15) + 1
If j15 <= 0 Or j15 > 4 Then GoTo 150
If b(5, j15) = 0 Then b(5, j15) = j15: c(5, 1) = j15 Else GoTo 150
For j14 = 1 To 4 'a(14)
If b(4, j14) = 0 Then b(4, j14) = j14: c(4, 2) = j14 Else GoTo 140
a(14) = j14 - 1
a(13) = 6 - a(14) - a(19) - a(20)
j13 = a(13) + 1
If j13 <= 0 Or j13 > 4 Then GoTo 130
If b(4, j13) = 0 Then b(4, j13) = j13: c(4, 1) = j13 Else GoTo 130
For j12 = 1 To 4 'a(12)
If b(3, j12) = 0 Then b(3, j12) = j12: c(3, 4) = j12 Else GoTo 120
a(12) = j12 - 1
a(11) = 6 + a(12) - a(16) + a(18) - a(21) + a(24) - a(26) - a(29) - a(31) - a(35)
j11 = a(11) + 1
If j11 <= 0 Or j11 > 4 Then GoTo 115
If b(3, j11) = 0 Then b(3, j11) = j11: c(3, 3) = j11 Else GoTo 115
a(6) = 3 - a(12) - a(18) - a(24) + a(29) + a(35)
j6 = a(6) + 1
If j6 <= 0 Or j6 > 4 Then GoTo 110
If b(3, j6) = 0 Then b(3, j6) = j6: c(3, 2) = j6 Else GoTo 110
a(5) = -3 - a(12) + a(16) + a(21) + a(26) + a(31)
j5 = a(5) + 1
If j5 <= 0 Or j5 > 4 Then GoTo 105
If b(3, j5) = 0 Then b(3, j5) = j5: c(3, 1) = j5 Else GoTo 105
For j10 = 1 To 4 'a(10)
If b(2, j10) = 0 Then b(2, j10) = j10: c(2, 4) = j10 Else GoTo 100
a(10) = j10 - 1
a(4) = 9 - a(10) - a(16) - a(22) - a(28) - a(34)
j4 = a(4) + 1
If j4 <= 0 Or j4 > 4 Then GoTo 95
If b(2, j4) = 0 Then b(2, j4) = j4: c(2, 2) = j4 Else GoTo 95
For j9 = 1 To 4 'a(9)
If b(2, j9) = 0 Then b(2, j9) = j9: c(2, 3) = j9 Else GoTo 90
a(9) = j9 - 1
a(8) = 9 - 0.5*a(9) - 0.5*a(10) - a(12) - 0.5*a(14) + a(16) - 0.5*a(18) - 0.5*a(20) +
+ a(21) - 0.5*a(24) - a(25) - a(26) - 0.5*a(27) - 0.5*a(28) - a(32) + a(35)
If a(8) < 0 Or a(8) > 3 Or Int(a(8)) <> a(8) Then GoTo 80
j8 = a(8) + 1
If b(1, j8) = 0 Then b(1, j8) = j8: c(1, 4) = j8 Else GoTo 80
a(7) = a(8) + a(14) - a(16) + a(20) - a(21) - a(25) + a(29) - a(31) + a(36)
If a(7) < 0 Or a(7) > 3 Or Int(a(7)) <> a(7) Then GoTo 75
j7 = a(7) + 1
If b(1, j7) = 0 Then b(1, j7) = j7: c(1, 3) = j7 Else GoTo 75
a(3) = -3 - a(9) + a(16) + a(22) + a(28) + a(34)
If a(3) < 0 Or a(3) > 3 Then GoTo 70
j3 = a(3) + 1
If b(2, j3) = 0 Then b(2, j3) = j3: c(2, 1) = j3 Else GoTo 70
a(2) = 3 - a(8) - a(14) - a(20) + a(25) + a(31)
If a(2) < 0 Or a(2) > 3 Or Int(a(2)) <> a(2) Then GoTo 65
j2 = a(2) + 1
If b(1, j2) = 0 Then b(1, j2) = j2: c(1, 2) = j2 Else GoTo 65
a(1) = -3 + a(2) + a(14) + a(16) + a(20) + a(21) + a(26) + a(27) + a(28) - a(29) - a(31) - a(35) - 2 * a(36)
If a(1) < 0 Or a(1) > 3 Or Int(a(1)) <> a(1) Then GoTo 60
j1 = a(1) + 1
If b(1, j1) = 0 Then b(1, j1) = j1: c(1, 1) = j1 Else GoTo 60
' n9 = n9 + 1: GoSub 640 'Print results (selected numbers)
' n9 = n9 + 1: GoSub 650 'Print results (Medjig Squares)
GoSub 700 'Calculate Magic Squares
GoSub 750: If fl1 = 0 Then GoTo 55 'Check Symmetrical Diagonals
' GoSub 760: If n6 < 16 Then GoTo 55 'Almost Associated (16 pairs)
n9 = n9 + 1: Cells(1, 1).Value = n9 'Counting
' n9 = n9 + 1: GoSub 650 'Print results (Medjig Squares)
' n9 = n9 + 1: GoSub 710 'Print results (Magic Squares)
55 b(1, c(1, 1)) = 0: c(1, 1) = 0
60 b(1, c(1, 2)) = 0: c(1, 2) = 0
65 b(2, c(2, 1)) = 0: c(2, 1) = 0
70 b(1, c(1, 3)) = 0: c(1, 3) = 0
75 b(1, c(1, 4)) = 0: c(1, 4) = 0
80 b(2, c(2, 3)) = 0: c(2, 3) = 0
90 Next j9
b(2, c(2, 2)) = 0: c(2, 2) = 0
95 b(2, c(2, 4)) = 0: c(2, 4) = 0
100 Next j10
b(3, c(3, 1)) = 0: c(3, 1) = 0
105 b(3, c(3, 2)) = 0: c(3, 2) = 0
110 b(3, c(3, 3)) = 0: c(3, 3) = 0
115 b(3, c(3, 4)) = 0: c(3, 4) = 0
120 Next j12
b(4, c(4, 1)) = 0: c(4, 1) = 0
130 b(4, c(4, 2)) = 0: c(4, 2) = 0
140 Next j14
b(5, c(5, 1)) = 0: c(5, 1) = 0
150 b(5, c(5, 2)) = 0: c(5, 2) = 0
160 Next j16
b(6, c(6, 1)) = 0: c(6, 1) = 0
170 b(6, c(6, 2)) = 0: c(6, 2) = 0
180 Next j18
b(4, c(4, 3)) = 0: c(4, 3) = 0
190 b(4, c(4, 4)) = 0: c(4, 4) = 0
200 Next j20
b(5, c(5, 3)) = 0: c(5, 3) = 0
210 Next j21
b(5, c(5, 4)) = 0: c(5, 4) = 0
220 Next j22
b(6, c(6, 3)) = 0: c(6, 3) = 0
230 Next j23
b(6, c(6, 4)) = 0: c(6, 4) = 0
240 Next j24
b(7, c(7, 1)) = 0: c(7, 1) = 0
250 b(7, c(7, 2)) = 0: c(7, 2) = 0
260 Next j26
b(8, c(8, 1)) = 0: c(8, 1) = 0
270 b(8, c(8, 2)) = 0: c(8, 2) = 0
280 Next j28
b(9, c(9, 1)) = 0: c(9, 1) = 0
290 b(9, c(9, 2)) = 0: c(9, 2) = 0
300 Next j30
b(7, c(7, 3)) = 0: c(7, 3) = 0
310 b(7, c(7, 4)) = 0: c(7, 4) = 0
320 Next j32
b(8, c(8, 3)) = 0: c(8, 3) = 0
330 Next j33
b(8, c(8, 4)) = 0: c(8, 4) = 0
340 Next j34
b(9, c(9, 3)) = 0: c(9, 3) = 0
350 Next j35
b(9, c(9, 4)) = 0: c(9, 4) = 0
360 Next j36
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Combinations"
y = MsgBox(t10, 0, "Routine MgcSqr6d")
End
Return
' Print results (selected numbers)
640 For i1 = 1 To 36
Cells(n9, i1).Value = a(i1)
Next i1
Return
' Print results (squares)
650 n2 = n2 + 1
If n2 = 6 Then
n2 = 1: k1 = k1 + 7: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 7
End If
Cells(k1, k2 + 1).Select: Cells(k1, k2 + 1).Value = n9
i3 = 0
For i1 = 1 To 6
For i2 = 1 To 6
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a(i3)
Next i2
Next i1
Return
' Calculate Magic Square c1()
700 c1(1) = b1(1) + 9 * a(1): c1(2) = b1(1) + 9 * a(2): c1(3) = b1(2) + 9 * a(3)
c1(4) = b1(2) + 9 * a(4): c1(5) = b1(3) + 9 * a(5): c1(6) = b1(3) + 9 * a(6)
c1(7) = b1(1) + 9 * a(7): c1(8) = b1(1) + 9 * a(8): c1(9) = b1(2) + 9 * a(9)
c1(10) = b1(2) + 9 * a(10): c1(11) = b1(3) + 9 * a(11): c1(12) = b1(3) + 9 * a(12)
c1(13) = b1(4) + 9 * a(13): c1(14) = b1(4) + 9 * a(14): c1(15) = b1(5) + 9 * a(15)
c1(16) = b1(5) + 9 * a(16): c1(17) = b1(6) + 9 * a(17): c1(18) = b1(6) + 9 * a(18)
c1(19) = b1(4) + 9 * a(19): c1(20) = b1(4) + 9 * a(20): c1(21) = b1(5) + 9 * a(21)
c1(22) = b1(5) + 9 * a(22): c1(23) = b1(6) + 9 * a(23): c1(24) = b1(6) + 9 * a(24)
c1(25) = b1(7) + 9 * a(25): c1(26) = b1(7) + 9 * a(26): c1(27) = b1(8) + 9 * a(27)
c1(28) = b1(8) + 9 * a(28): c1(29) = b1(9) + 9 * a(29): c1(30) = b1(9) + 9 * a(30)
c1(31) = b1(7) + 9 * a(31): c1(32) = b1(7) + 9 * a(32): c1(33) = b1(8) + 9 * a(33)
c1(34) = b1(8) + 9 * a(34): c1(35) = b1(9) + 9 * a(35): c1(36) = b1(9) + 9 * a(36)
Return
' Print Magic Square c1()
710 n2 = n2 + 1
If n2 = 6 Then
n2 = 1: k1 = k1 + 7: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 7
End If
Cells(k1, k2 + 1).Select: Cells(k1, k2 + 1).Value = n9
i3 = 0
For i1 = 1 To 6
For i2 = 1 To 6
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = c1(i3)
Next i2
Next i1
Return
' Check Symmetrical Diagonals
750 fl1 = 1
s(1) = c1(1) + c1(36): s(2) = c1(8) + c1(29): s(3) = c1(15) + c1(22)
s(4) = c1(6) + c1(31): s(5) = c1(11) + c1(26): s(6) = c1(16) + c1(21)
For i1 = 1 To 6
If s(i1) <> 37 Then fl1 = 0: Return
Next i1
Return
' Almost Associated
760 fl1 = 0
s(1) = c1(1) + c1(36): s(7) = c1(7) + c1(30): s(13) = c1(13) + c1(24):
s(2) = c1(2) + c1(35): s(8) = c1(8) + c1(29): s(14) = c1(14) + c1(23):
s(3) = c1(3) + c1(34): s(9) = c1(9) + c1(28): s(15) = c1(15) + c1(22):
s(4) = c1(4) + c1(33): s(10) = c1(10) + c1(27): s(16) = c1(16) + c1(21):
s(5) = c1(5) + c1(32): s(11) = c1(11) + c1(26): s(17) = c1(17) + c1(20):
s(6) = c1(6) + c1(31): s(12) = c1(12) + c1(25): s(18) = c1(18) + c1(19):
n6 = 0
For i1 = 1 To 18
If s(i1) = 37 Then n6 = n6 + 1
Next i1
Return
End Sub