' Generates Eccentric Magic Squares of order 8, based on supplementar rows and columns
' Tested with Office 2007 under Windows 7
Sub MgcSqr8j()
Dim a1(36), a(64), b(64), c(64)
y = MsgBox("Locked", vbCritical, "Routine MgcSqr8j")
End
' Defining Eccentric Magic Square (6 x 6)
a1(1) = 5: a1(2) = 8: a1(3) = 7: a1(4) = 28: a1(5) = 36: a1(6) = 27
a1(7) = 29: a1(8) = 32: a1(9) = 30: a1(10) = 9: a1(11) = 1: a1(12) = 10
a1(13) = 3: a1(14) = 34: a1(15) = 18: a1(16) = 21: a1(17) = 24: a1(18) = 11
a1(19) = 6: a1(20) = 31: a1(21) = 23: a1(22) = 12: a1(23) = 17: a1(24) = 22
a1(25) = 33: a1(26) = 4: a1(27) = 13: a1(28) = 26: a1(29) = 19: a1(30) = 16
a1(31) = 35: a1(32) = 2: a1(33) = 20: a1(34) = 15: a1(35) = 14: a1(36) = 25
n2 = 0: n9 = 0
m1 = 1: m2 = 64: s1 = 260
Sheets("Klad1").Select
t1 = Timer
' Initiate a(..)
For j1 = 19 To 64
Select Case j1
Case 19 To 24
a(j1) = a1(j1 - 18) + 14: b(a(j1)) = a(j1): c(j1) = a(j1)
Case 27 To 32
a(j1) = a1(j1 - 20) + 14: b(a(j1)) = a(j1): c(j1) = a(j1)
Case 35 To 40
a(j1) = a1(j1 - 22) + 14: b(a(j1)) = a(j1): c(j1) = a(j1)
Case 43 To 48
a(j1) = a1(j1 - 24) + 14: b(a(j1)) = a(j1): c(j1) = a(j1)
Case 51 To 56
a(j1) = a1(j1 - 26) + 14: b(a(j1)) = a(j1): c(j1) = a(j1)
Case 59 To 64
a(j1) = a1(j1 - 28) + 14: b(a(j1)) = a(j1): c(j1) = a(j1)
End Select
Next j1
For j58 = m1 To m2 'a(58)
If b(j58) = 0 Then b(j58) = j58: c(58) = j58 Else GoTo 580
a(58) = j58
a(57) = 65 - a(58)
If a(57) <= 0 Or a(57) > 64 Then GoTo 570
If b(a(57)) = 0 Then b(a(57)) = a(57): c(57) = a(57) Else GoTo 570
For j50 = 51 To 51 'a(50)
If b(j50) = 0 Then b(j50) = j50: c(50) = j50 Else GoTo 500
a(50) = j50
a(49) = 65 - a(50)
If a(49) <= 0 Or a(49) > 64 Then GoTo 490
If b(a(49)) = 0 Then b(a(49)) = a(49): c(49) = a(49) Else GoTo 490
For j42 = m1 To m2 'a(42)
If b(j42) = 0 Then b(j42) = j42: c(42) = j42 Else GoTo 420
a(42) = j42
a(41) = 65 - a(42)
If a(41) <= 0 Or a(41) > 64 Then GoTo 410
If b(a(41)) = 0 Then b(a(41)) = a(41): c(41) = a(41) Else GoTo 410
For j34 = m1 To m2 'a(34)
If b(j34) = 0 Then b(j34) = j34: c(34) = j34 Else GoTo 340
a(34) = j34
a(33) = 65 - a(34)
If a(33) <= 0 Or a(33) > 64 Then GoTo 330
If b(a(33)) = 0 Then b(a(33)) = a(33): c(33) = a(33) Else GoTo 330
For j26 = m1 To m2 'a(26)
If b(j26) = 0 Then b(j26) = j26: c(26) = j26 Else GoTo 260
a(26) = j26
a(25) = 65 - a(26)
If a(25) <= 0 Or a(25) > 64 Then GoTo 250
If b(a(25)) = 0 Then b(a(25)) = a(25): c(25) = a(25) Else GoTo 250
For j18 = m1 To m2 'a(18)
If b(j18) = 0 Then b(j18) = j18: c(18) = j18 Else GoTo 180
a(18) = j18
a(17) = 65 - a(18)
If a(17) <= 0 Or a(17) > 64 Then GoTo 170
If b(a(17)) = 0 Then b(a(17)) = a(17): c(17) = a(17) Else GoTo 170
For j16 = 64 To 64 'a(16)
If b(j16) = 0 Then b(j16) = j16: c(16) = j16 Else GoTo 160
a(16) = j16
a(15) = 195 + a(16) - a(22) - a(29) - a(36) - a(43) - a(50) - a(57)
If a(15) <= 0 Or a(15) > 64 Then GoTo 150
If b(a(15)) = 0 Then b(a(15)) = a(15): c(15) = a(15) Else GoTo 150
a(8) = 65 - a(16)
If a(8) <= 0 Or a(8) > 64 Then GoTo 80
If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 80
a(7) = 65 - a(15)
If a(7) <= 0 Or a(7) > 64 Then GoTo 70
If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 70
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(6) = 65 - a(14)
If a(6) <= 0 Or a(6) > 64 Then GoTo 60
If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 60
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(5) = 65 - a(13)
If a(5) <= 0 Or a(5) > 64 Then GoTo 50
If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 50
For j12 = m1 To m2 'a(12)
If b(j12) = 0 Then b(j12) = j12: c(12) = j12 Else GoTo 120
a(12) = j12
a(4) = 65 - a(12)
If a(4) <= 0 Or a(4) > 64 Then GoTo 40
If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 40
For j11 = m1 To m2 'a(11)
If b(j11) = 0 Then b(j11) = j11: c(11) = j11 Else GoTo 110
a(11) = j11
a(10) = 0.5 * (65 - a(11) - a(12) - a(13) - a(14) - a(15) - a(16) + a(17) + a(25) + a(33) + a(41) + a(49) + a(57))
If a(10) <= 0 Or a(10) > 64 Or Int(a(10)) <> a(10) Then GoTo 100
If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 100
a(9) = 260 - a(10) - a(11) - a(12) - a(13) - a(14) - a(15) - a(16)
If a(9) <= 0 Or a(9) > 64 Then GoTo 90
If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 90
a(3) = 65 - a(11)
If a(3) <= 0 Or a(3) > 64 Then GoTo 30
If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 30
a(2) = 65 - a(9)
If a(2) <= 0 Or a(2) > 64 Then GoTo 20
If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 20
a(1) = 65 - a(10)
If a(1) <= 0 Or a(1) > 64 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(9)) = 0: c(9) = 0
90 b(c(10)) = 0: c(10) = 0
100 b(c(11)) = 0: c(11) = 0
110 Next j11
b(c(4)) = 0: c(4) = 0
40 b(c(12)) = 0: c(12) = 0
120 Next j12
b(c(5)) = 0: c(5) = 0
50 b(c(13)) = 0: c(13) = 0
130 Next j13
b(c(6)) = 0: c(6) = 0
60 b(c(14)) = 0: c(14) = 0
140 Next j14
b(c(7)) = 0: c(7) = 0
70 b(c(8)) = 0: c(8) = 0
80 b(c(15)) = 0: c(15) = 0
150 b(c(16)) = 0: c(16) = 0
160 Next j16
b(c(17)) = 0: c(17) = 0
170 b(c(18)) = 0: c(18) = 0
180 Next j18
b(c(25)) = 0: c(25) = 0
250 b(c(26)) = 0: c(26) = 0
260 Next j26
b(c(33)) = 0: c(33) = 0
330 b(c(34)) = 0: c(34) = 0
340 Next j34
b(c(41)) = 0: c(41) = 0
410 b(c(42)) = 0: c(42) = 0
420 Next j42
b(c(49)) = 0: c(49) = 0
490 b(c(50)) = 0: c(50) = 0
500 Next j50
b(c(57)) = 0: c(57) = 0
570 b(c(58)) = 0: c(58) = 0
580 Next j58
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine MgcSqr8j")
End
' Print results (selected numbers)
2645 For i1 = 1 To 64
Cells(n9, i1).Value = a(i1)
Next i1
Return
' Print results (squares)
2650 n2 = n2 + 1
If n2 = 5 Then
n2 = 1: k1 = k1 + 9: k2 = 0
Else
If n9 > 1 Then k2 = k2 + 9
End If
Cells(k1 + 1, k2 + 1).Select
i3 = 0
For i1 = 1 To 8
For i2 = 1 To 8
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a(i3)
Next i2
Next i1
Return
End Sub