' Generates Eccentric Magic Squares of order 9, based on supplementar rows and columns
' Tested with Office 2007 under Windows 7
Sub MgcSqr9e()
Dim a1(49), a(81), b(81), c(81)
y = MsgBox("Locked", vbCritical, "Routine MgcSqr9e")
End
' Defining Pan Magic Square (7 x 7)
'a1(1) = 11: a1(2) = 16: a1(3) = 28: a1(4) = 33: a1(5) = 38: a1(6) = 43: a1(7) = 6
'a1(8) = 47: a1(9) = 3: a1(10) = 8: a1(11) = 20: a1(12) = 25: a1(13) = 30: a1(14) = 42
'a1(15) = 34: a1(16) = 39: a1(17) = 44: a1(18) = 7: a1(19) = 12: a1(20) = 17: a1(21) = 22
'a1(22) = 21: a1(23) = 26: a1(24) = 31: a1(25) = 36: a1(26) = 48: a1(27) = 4: a1(28) = 9
'a1(29) = 1: a1(30) = 13: a1(31) = 18: a1(32) = 23: a1(33) = 35: a1(34) = 40: a1(35) = 45
'a1(36) = 37: a1(37) = 49: a1(38) = 5: a1(39) = 10: a1(40) = 15: a1(41) = 27: a1(42) = 32
'a1(43) = 24: a1(44) = 29: a1(45) = 41: a1(46) = 46: a1(47) = 2: a1(48) = 14: a1(49) = 19
' Defining Eccentric Square (7 x 7)
a1(1) = 8: a1(2) = 38: a1(3) = 2: a1(4) = 3: a1(5) = 40: a1(6) = 39: a1(7) = 45
a1(8) = 12: a1(9) = 42: a1(10) = 48: a1(11) = 47: a1(12) = 10: a1(13) = 11: a1(14) = 5
a1(15) = 7: a1(16) = 43: a1(17) = 17: a1(18) = 31: a1(19) = 15: a1(20) = 32: a1(21) = 30
a1(22) = 9: a1(23) = 41: a1(24) = 19: a1(25) = 33: a1(26) = 35: a1(27) = 18: a1(28) = 20
a1(29) = 46: a1(30) = 4: a1(31) = 16: a1(32) = 34: a1(33) = 28: a1(34) = 23: a1(35) = 24
a1(36) = 44: a1(37) = 6: a1(38) = 37: a1(39) = 13: a1(40) = 21: a1(41) = 25: a1(42) = 29
a1(43) = 49: a1(44) = 1: a1(45) = 36: a1(46) = 14: a1(47) = 26: a1(48) = 27: a1(49) = 22
n2 = 0: n9 = 0
m1 = 1: m2 = 81: s1 = 369
Sheets("Klad1").Select
t1 = Timer
' Initiate a(..)
For j1 = 21 To 81
Select Case j1
Case 21 To 27
a(j1) = a1(j1 - 20) + 16: b(a(j1)) = a(j1): c(j1) = a(j1)
Case 30 To 36
a(j1) = a1(j1 - 22) + 16: b(a(j1)) = a(j1): c(j1) = a(j1)
Case 39 To 45
a(j1) = a1(j1 - 24) + 16: b(a(j1)) = a(j1): c(j1) = a(j1)
Case 48 To 54
a(j1) = a1(j1 - 26) + 16: b(a(j1)) = a(j1): c(j1) = a(j1)
Case 57 To 63
a(j1) = a1(j1 - 28) + 16: b(a(j1)) = a(j1): c(j1) = a(j1)
Case 66 To 72
a(j1) = a1(j1 - 30) + 16: b(a(j1)) = a(j1): c(j1) = a(j1)
Case 75 To 81
a(j1) = a1(j1 - 32) + 16: b(a(j1)) = a(j1): c(j1) = a(j1)
End Select
Next j1
For j74 = 80 To 80 'a(74)
If b(j74) = 0 Then b(j74) = j74: c(74) = j74 Else GoTo 740
a(74) = j74
a(73) = 82 - a(74)
If a(73) <= 0 Or a(73) > 81 Then GoTo 730
If b(a(73)) = 0 Then b(a(73)) = a(73): c(73) = a(73) Else GoTo 730
For j65 = 78 To 78 'a(65)
If b(j65) = 0 Then b(j65) = j65: c(65) = j65 Else GoTo 650
a(65) = j65
a(64) = 82 - a(65)
If a(64) <= 0 Or a(64) > 81 Then GoTo 640
If b(a(64)) = 0 Then b(a(64)) = a(64): c(64) = a(64) Else GoTo 640
For j56 = 77 To 77 'a(56)
If b(j56) = 0 Then b(j56) = j56: c(56) = j56 Else GoTo 560
a(56) = j56
a(55) = 82 - a(56)
If a(55) <= 0 Or a(55) > 81 Then GoTo 550
If b(a(55)) = 0 Then b(a(55)) = a(55): c(55) = a(55) Else GoTo 550
For j47 = m1 To m2 'a(47)
If b(j47) = 0 Then b(j47) = j47: c(47) = j47 Else GoTo 470
a(47) = j47
a(46) = 82 - a(47)
If a(46) <= 0 Or a(46) > 81 Then GoTo 460
If b(a(46)) = 0 Then b(a(46)) = a(46): c(46) = a(46) Else GoTo 460
For j38 = m1 To m2 'a(38)
If b(j38) = 0 Then b(j38) = j38: c(38) = j38 Else GoTo 380
a(38) = j38
a(37) = 82 - a(38)
If a(37) <= 0 Or a(37) > 81 Then GoTo 370
If b(a(37)) = 0 Then b(a(37)) = a(37): c(37) = a(37) Else GoTo 370
For j29 = m1 To m2 'a(29)
If b(j29) = 0 Then b(j29) = j29: c(29) = j29 Else GoTo 290
a(29) = j29
a(28) = 82 - a(29)
If a(28) <= 0 Or a(28) > 81 Then GoTo 280
If b(a(28)) = 0 Then b(a(28)) = a(28): c(28) = a(28) Else GoTo 280
For j20 = m1 To m2 'a(20)
If b(j20) = 0 Then b(j20) = j20: c(20) = j20 Else GoTo 200
a(20) = j20
a(19) = 82 - a(20)
If a(19) <= 0 Or a(19) > 81 Then GoTo 190
If b(a(19)) = 0 Then b(a(19)) = a(19): c(19) = a(19) Else GoTo 190
For j18 = 81 To 81 'a(18)
If b(j18) = 0 Then b(j18) = j18: c(18) = j18 Else GoTo 180
a(18) = j18
a(17) = 287 + a(18) - a(25) - a(33) - a(41) - a(49) - a(57) - a(65) - a(73)
If a(17) <= 0 Or a(17) > 81 Then GoTo 170
If b(a(17)) = 0 Then b(a(17)) = a(17): c(17) = a(17) Else GoTo 170
a(9) = 82 - a(18)
If a(9) <= 0 Or a(9) > 81 Then GoTo 90
If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 90
a(8) = 82 - a(17)
If a(8) <= 0 Or a(8) > 81 Then GoTo 80
If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 80
For j16 = 76 To 76 'a(16)
If b(j16) = 0 Then b(j16) = j16: c(16) = j16 Else GoTo 160
a(16) = j16
a(7) = 82 - a(16)
If a(7) <= 0 Or a(7) > 81 Then GoTo 70
If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 70
For j15 = 74 To 74 'a(15)
If b(j15) = 0 Then b(j15) = j15: c(15) = j15 Else GoTo 150
a(15) = j15
a(6) = 82 - a(15)
If a(6) <= 0 Or a(6) > 81 Then GoTo 60
If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 60
For j14 = m1 To m2 'a(14)
If b(j14) = 0 Then b(j14) = j14: c(14) = j14 Else GoTo 140
a(14) = j14
a(5) = 82 - a(14)
If a(5) <= 0 Or a(5) > 81 Then GoTo 50
If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 50
For j13 = m1 To m2 'a(13)
If b(j13) = 0 Then b(j13) = j13: c(13) = j13 Else GoTo 130
a(13) = j13
a(4) = 82 - a(13)
If a(4) <= 0 Or a(4) > 81 Then GoTo 40
If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 40
For j12 = m1 To m2 'a(12)
If b(j12) = 0 Then b(j12) = j12: c(12) = j12 Else GoTo 120
a(12) = j12
n10 = n10 + 1: Cells(1, 41).Value = n10 'Prevent Time Out
a(11) = 41 + 0.5 * (-a(12) - a(13) - a(14) - a(15) - a(16) - a(17) - a(18) + a(19) + a(28) + a(37) + a(46) + a(55) + a(64) + a(73))
If a(11) <= 0 Or a(11) > 81 Or Int(a(11)) <> a(11) Then GoTo 110
If b(a(11)) = 0 Then b(a(11)) = a(11): c(11) = a(11) Else GoTo 110
a(10) = 369 - a(11) - a(12) - a(13) - a(14) - a(15) - a(16) - a(17) - a(18)
If a(10) <= 0 Or a(10) > 81 Then GoTo 100
If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 100
a(3) = 82 - a(12)
If a(3) <= 0 Or a(3) > 81 Then GoTo 30
If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 30
a(2) = 82 - a(10)
If a(2) <= 0 Or a(2) > 81 Then GoTo 20
If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 20
a(1) = 82 - a(11)
If a(1) <= 0 Or a(1) > 81 Then GoTo 10
If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 10
n9 = n9 + 1
GoSub 2650 'Print results (squares)
' GoSub 2645 'Print results (selected numbers)
b(c(1)) = 0: c(1) = 0
10 b(c(2)) = 0: c(2) = 0
20 b(c(3)) = 0: c(3) = 0
30 b(c(10)) = 0: c(10) = 0
100 b(c(11)) = 0: c(11) = 0
110 b(c(12)) = 0: c(12) = 0
120 Next j12
b(c(4)) = 0: c(4) = 0
40 b(c(13)) = 0: c(13) = 0
130 Next j13
b(c(5)) = 0: c(5) = 0
50 b(c(14)) = 0: c(14) = 0
140 Next j14
b(c(6)) = 0: c(6) = 0
60 b(c(15)) = 0: c(15) = 0
150 Next j15
b(c(7)) = 0: c(7) = 0
70 b(c(16)) = 0: c(16) = 0
160 Next j16
b(c(8)) = 0: c(8) = 0
80 b(c(9)) = 0: c(9) = 0
90 b(c(17)) = 0: c(17) = 0
170 b(c(18)) = 0: c(18) = 0
180 Next j18
b(c(19)) = 0: c(19) = 0
190 b(c(20)) = 0: c(20) = 0
200 Next j20
b(c(28)) = 0: c(28) = 0
280 b(c(29)) = 0: c(29) = 0
290 Next j29
b(c(37)) = 0: c(37) = 0
370 b(c(38)) = 0: c(38) = 0
380 Next j38
b(c(46)) = 0: c(46) = 0
460 b(c(47)) = 0: c(47) = 0
470 Next j47
b(c(55)) = 0: c(55) = 0
550 b(c(56)) = 0: c(56) = 0
560 Next j56
b(c(64)) = 0: c(64) = 0
640 b(c(65)) = 0: c(65) = 0
650 Next j65
b(c(73)) = 0: c(73) = 0
730 b(c(74)) = 0: c(74) = 0
740 Next j74
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s1)
y = MsgBox(t10, 0, "Routine MgcSqr9e")
End
' Print results (selected numbers)
2645 For i1 = 1 To 81
Cells(n9, i1).Value = a(i1)
Next i1
Return
' Print results (squares)
2650 n2 = n2 + 1
If n2 = 5 Then
n2 = 1: k1 = k1 + 10: k2 = 0
Else
If n9 > 1 Then k2 = k2 + 10
End If
Cells(k1 + 1, k2 + 1).Select
i3 = 0
For i1 = 1 To 9
For i2 = 1 To 9
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a(i3)
Next i2
Next i1
Return
End Sub