' Generates Magic Squares of order 5 for integers 1 thru 25
' Preselected Center Lines
' Tested with Office 2007 under Windows 7
Sub MgcSqr5k()
Dim a(25), b(25), c(25), a1(5), a2(5)
Dim b1(25), b2(25), b3(25), d(5), Ln5(5)
y = MsgBox("Locked", vbCritical, "Routine MgcSqr5k")
End
n2 = 0: n9 = 0: k1 = 1: k2 = 1
m1 = 1: m2 = 25: s1 = 65
' Generate data
Sheets("Klad1").Select
t1 = Timer
For j100 = 2 To 16 'Axes
Cells(k1, 1).Select: Cells(k1, 1).Value = j100
a(13) = Sheets("MgcLns5b").Cells(j100, 2).Value 'Center
b(a(13)) = a(13)
Erase b1, b2
For i1 = 1 To 4
a1(i1) = Sheets("MgcLns5b").Cells(j100, i1 + 3).Value
b1(a1(i1)) = a1(i1)
a2(i1) = Sheets("MgcLns5b").Cells(j100, i1 + 8).Value
b2(a2(i1)) = a2(i1)
Next i1
' 3, 8, 18, 23
For j3 = 1 To 4 'a(3)
If b(a1(j3)) = 0 Then b(a1(j3)) = a1(j3): c(3) = a1(j3) Else GoTo 30
a(3) = a1(j3)
For j8 = 1 To 4 'a(8)
If b(a1(j8)) = 0 Then b(a1(j8)) = a1(j8): c(8) = a1(j8) Else GoTo 80
a(8) = a1(j8)
For j18 = 1 To 4 'a(18)
If b(a1(j18)) = 0 Then b(a1(j18)) = a1(j18): c(18) = a1(j18) Else GoTo 180
a(18) = a1(j18)
a(23) = 65 - a(18) - a(13) - a(8) - a(3)
If a(23) < a1(1) Or a(23) > a1(4) Then GoTo 230
If b1(a(23)) = 0 Then GoTo 230
If b(a(23)) = 0 Then b(a(23)) = a(23): c(23) = a(23) Else GoTo 230
' 11, 12, 14, 15
For j11 = 1 To 4 'a(11)
If b(a2(j11)) = 0 Then b(a2(j11)) = a2(j11): c(11) = a2(j11) Else GoTo 110
a(11) = a2(j11)
For j12 = 1 To 4 'a(12)
If b(a2(j12)) = 0 Then b(a2(j12)) = a2(j12): c(12) = a2(j12) Else GoTo 120
a(12) = a2(j12)
For j14 = 1 To 4 'a(14)
If b(a2(j14)) = 0 Then b(a2(j14)) = a2(j14): c(14) = a2(j14) Else GoTo 140
a(14) = a2(j14)
a(15) = 65 - a(14) - a(13) - a(12) - a(11)
If a(15) < a2(1) Or a(15) > a2(4) Then GoTo 150
If b2(a(15)) = 0 Then GoTo 150
If b(a(15)) = 0 Then b(a(15)) = a(15): c(15) = a(15) Else GoTo 150
For j25 = m1 To m2 'a(25)
If b(j25) = 0 Then b(j25) = j25: c(25) = j25 Else GoTo 250
a(25) = j25
For j19 = m1 To m2 'a(19)
If b(j19) = 0 Then b(j19) = j19: c(19) = j19 Else GoTo 190
a(19) = j19
For j7 = m1 To m2 'a(7)
If b(j7) = 0 Then b(j7) = j7: c(7) = j7 Else GoTo 70
a(7) = j7
a(1) = 65 - a(7) - a(13) - a(19) - a(25)
If a(1) <= 0 Or a(1) > 25 Then GoTo 10
If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 10
' Check Dia Ltop/Rbtm (optional)
d(1) = a(25): d(2) = a(19): d(3) = a(13): d(4) = a(7): d(5) = a(1):
GoSub 2500: If fl1 = 0 Then GoTo 15
For j21 = m1 To m2 'a(21)
If b(j21) = 0 Then b(j21) = j21: c(21) = j21 Else GoTo 210
a(21) = j21
For j17 = m1 To m2 'a(17)
If b(j17) = 0 Then b(j17) = j17: c(17) = j17 Else GoTo 170
a(17) = j17
a(9) = (s1 - 2 * a(17) + a(1) - a(7) - a(19) + a(25) - a(14) - a(12) + a(23) - a(13) + a(3)) / 2
If a(9) <= 0 Or a(9) > 25 Or CInt(a(9)) <> a(9) Then GoTo 90
If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 90
a(5) = 65 - a(9) - a(13) - a(17) - a(21)
If a(5) <= 0 Or a(5) > 25 Then GoTo 50
If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 50
' Check Dia Ltop/Rbtm (optional)
d(1) = a(21): d(2) = a(17): d(3) = a(13): d(4) = a(9): d(5) = a(5):
GoSub 2500: If fl1 = 0 Then GoTo 55
For j24 = m1 To m2 'a(24)
If b(j24) = 0 Then b(j24) = j24: c(24) = j24 Else GoTo 240
a(24) = j24
a(22) = 65 - a(24) - a(21) - a(25) - a(23)
If a(22) <= 0 Or a(22) > 25 Then GoTo 220
If b(a(22)) = 0 Then b(a(22)) = a(22): c(22) = a(22) Else GoTo 220
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(16) = 65 - a(20) - a(17) - a(19) - a(18)
If a(16) <= 0 Or a(16) > 25 Then GoTo 160
If b(a(16)) = 0 Then b(a(16)) = a(16): c(16) = a(16) Else GoTo 160
a(10) = 65 - a(20) - a(5) - a(25) - a(15)
If a(10) <= 0 Or a(10) > 25 Then GoTo 100
If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 100
a(6) = 65 - a(16) - a(21) - a(1) - a(11)
If a(6) <= 0 Or a(6) > 25 Then GoTo 60
If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 60
a(4) = 65 - a(24) - a(9) - a(19) - a(14)
If a(4) <= 0 Or a(4) > 25 Then GoTo 40
If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 40
a(2) = 65 - a(4) - a(5) - a(1) - a(3)
If a(2) <= 0 Or a(2) > 25 Then GoTo 20
If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 20
' Exclude solutions with identical numbers (Back Check)
GoSub 800: If fl1 = 0 Then GoTo 5
n9 = n9 + 1: GoSub 650 'Print results (squares)
' n9 = n9 + 1: GoSub 640 'Print results (selected numbers)
Erase b, c: GoTo 1000 'Print only first square
5
b(c(2)) = 0: c(2) = 0
20 b(c(4)) = 0: c(4) = 0
40 b(c(6)) = 0: c(6) = 0
60 b(c(10)) = 0: c(10) = 0
100 b(c(16)) = 0: c(16) = 0
160 b(c(20)) = 0: c(20) = 0
200 Next j20
b(c(22)) = 0: c(22) = 0
220 b(c(24)) = 0: c(24) = 0
240 Next j24
55
b(c(5)) = 0: c(5) = 0
50 b(c(9)) = 0: c(9) = 0
90 b(c(17)) = 0: c(17) = 0
170 Next j17
b(c(21)) = 0: c(21) = 0
210 Next j21
15
b(c(1)) = 0: c(1) = 0
10 b(c(7)) = 0: c(7) = 0
70 Next j7
b(c(19)) = 0: c(19) = 0
190 Next j19
b(c(25)) = 0: c(25) = 0
250 Next j25
b(c(15)) = 0: c(15) = 0
150 b(c(14)) = 0: c(14) = 0
140 Next j14
b(c(12)) = 0: c(12) = 0
120 Next j12
b(c(11)) = 0: c(11) = 0
110 Next j11
b(c(23)) = 0: c(23) = 0
230 b(c(18)) = 0: c(18) = 0
180 Next j18
b(c(8)) = 0: c(8) = 0
80 Next j8
b(c(3)) = 0: c(3) = 0
30 Next j3
1000 Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s1)
y = MsgBox(t10, 0, "Routine MgcSqr5k")
End
' Print results (selected numbers)
640 For i1 = 1 To 25
Cells(n9, i1).Value = a(i1)
Next i1
Cells(n9, 26).Value = n9
Cells(n9, 27).Value = j100
Cells(n9, 28).Select
Return
' Print results (squares)
650 n2 = n2 + 1
If n2 = 5 Then
n2 = 1: k1 = k1 + 6: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 6
End If
Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = n9
i3 = 0
For i1 = 1 To 5
For i2 = 1 To 5
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a(i3)
Next i2
Next i1
Return
' Exclude solutions with identical numbers
800 fl1 = 1
For j1 = 1 To 25
a20 = a(j1): If a20 = 0 Then GoTo 810
For j2 = (1 + j1) To 25
If a20 = a(j2) Then fl1 = 0: Return
Next j2
810 Next j1
Return
' Check diagonals (optional)
2500 fl1 = 1
Erase b3
For i1 = 1 To 5: b3(d(i1)) = d(i1): Next i1
i2 = 0: Erase d
For i1 = 1 To 25
If b3(i1) <> 0 Then i2 = i2 + 1: d(i2) = b3(i1)
Next i1
For i1 = 2 To 71
For i2 = 1 To 5
Ln5(i2) = Sheets("MgcLns5b").Cells(i1, i2 + 13).Value
Next i2
For i2 = 1 To 5
If d(i2) <> Ln5(i2) Then GoTo 2530
Next i2
Return 'Match
2530 Next i1
fl1 = 0 'No Match
Return
End Sub