' Generates Magic Squares of order 4, magic sum 130, 16 distinct integers, range i1(1) ... i1(16)
' Tested with Office 2007 under Windows 7
Sub MgcSqr4e()
Dim a(16), i1(16), b(64), c(64)
y = MsgBox("Locked", vbCritical, "Routine MgcSqr4e")
End
n2 = 0: n3 = 0: n9 = 0
m1 = 1: m2 = 16: s1 = 130
' Activate/replace applicable range
i1(1) = 4: i1(2) = 5: i1(3) = 59: i1(4) = 62
i1(5) = 57: i1(6) = 64: i1(7) = 2: i1(8) = 7
i1(9) = 6: i1(10) = 3: i1(11) = 61: i1(12) = 60
i1(13) = 63: i1(14) = 58: i1(15) = 8: i1(16) = 1
' i1(1) = 12: i1(2) = 13: i1(3) = 51: i1(4) = 54
' i1(5) = 49: i1(6) = 56: i1(7) = 10: i1(8) = 15
' i1(9) = 14: i1(10) = 11: i1(11) = 53: i1(12) = 52
' i1(13) = 55: i1(14) = 50: i1(15) = 16: i1(16) = 9
' i1(1) = 20: i1(2) = 21: i1(3) = 43: i1(4) = 46
' i1(5) = 41: i1(6) = 48: i1(7) = 18: i1(8) = 23
' i1(9) = 22: i1(10) = 19: i1(11) = 45: i1(12) = 44
' i1(13) = 47: i1(14) = 42: i1(15) = 24: i1(16) = 17
' i1(1) = 28: i1(2) = 29: i1(3) = 35: i1(4) = 38
' i1(5) = 33: i1(6) = 40: i1(7) = 26: i1(8) = 31
' i1(9) = 30: i1(10) = 27: i1(11) = 37: i1(12) = 36
' i1(13) = 39: i1(14) = 34: i1(15) = 32: i1(16) = 25
' Generate data
Sheets("Klad1").Select
t1 = Timer
For i16 = m1 To m2 'a(16)
j16 = i1(i16)
If b(j16) = 0 Then b(j16) = j16: c(16) = j16 Else GoTo 160
a(16) = j16
For i15 = m1 To m2 'a(15)
j15 = i1(i15)
If b(j15) = 0 Then b(j15) = j15: c(15) = j15 Else GoTo 150
a(15) = j15
For i14 = m1 To m2 'a(14)
j14 = i1(i14)
If b(j14) = 0 Then b(j14) = j14: c(14) = j14 Else GoTo 140
a(14) = j14
j13 = s1 - j14 - j15 - j16 'a(13)
a(13) = j13
b1 = 13: b2 = 13: GoSub 900: If fl1 = 0 Then GoTo 130
If b(j13) = 0 Then b(j13) = j13: c(13) = j13 Else GoTo 130
For i12 = m1 To m2 'a(12)
j12 = i1(i12)
If b(j12) = 0 Then b(j12) = j12: c(12) = j12 Else GoTo 120
a(12) = j12
For i11 = m1 To m2 'a(11)
j11 = i1(i11)
If b(j11) = 0 Then b(j11) = j11: c(11) = j11 Else GoTo 110
a(11) = j11
For i10 = m1 To m2 'a(10)
j10 = i1(i10)
If b(j10) = 0 Then b(j10) = j10: c(10) = j10 Else GoTo 100
a(10) = j10
j9 = s1 - j10 - j11 - j12 'a(9)
a(9) = j9
b1 = 9: b2 = 9: GoSub 900: If fl1 = 0 Then GoTo 90
If b(j9) = 0 Then b(j9) = j9: c(9) = j9 Else GoTo 90
For i8 = m1 To m2 'a(8)
j8 = i1(i8)
If b(j8) = 0 Then b(j8) = j8: c(8) = j8 Else GoTo 80
a(8) = j8
a(7) = a(8) - a(10) + a(12) - a(13) + a(16): If a(7) <= 0 Or a(7) > 64 Then GoTo 70
a(6) = s1 - a(8) - a(11) - a(12) + a(13) - a(16): If a(6) <= 0 Or a(6) > 64 Then GoTo 70
a(5) = -a(8) + a(10) + a(11): If a(5) <= 0 Or a(5) > 64 Then GoTo 70
a(4) = s1 - a(7) - a(10) - a(13): If a(4) <= 0 Or a(4) > 64 Then GoTo 70
a(3) = -s1 - a(8) + a(9) + 2 * a(10) + 2 * a(13) + a(14): If a(3) <= 0 Or a(3) > 64 Then GoTo 70
a(2) = a(8) - a(9) - 2 * a(10) + a(15) + 2 * a(16): If a(2) <= 0 Or a(2) > 64 Then GoTo 70
a(1) = a(8) + a(12) - a(13): If a(1) <= 0 Or a(1) > 64 Then GoTo 70
' Exclude solutions with identical numbers
GoSub 800: If fl1 = 0 Then GoTo 70
' Exclude solutions outside range i1(1) ... i1(16)
b1 = 1: b2 = 16: GoSub 900: If fl1 = 0 Then GoTo 70
n9 = n9 + 1
If n9 > 52428 Then n9 = 1: n3 = n3 + 20: k1 = -5: k2 = n3
GoSub 650 'Print results (squares)
' GoSub 640 'Print results (selected numbers)
70 b(c(8)) = 0: c(8) = 0
80 Next i8
b(c(9)) = 0: c(9) = 0
90 b(c(10)) = 0: c(10) = 0
100 Next i10
b(c(11)) = 0: c(11) = 0
110 Next i11
b(c(12)) = 0: c(12) = 0
120 Next i12
b(c(13)) = 0: c(13) = 0
130 b(c(14)) = 0: c(14) = 0
140 Next i14
b(c(15)) = 0: c(15) = 0
150 Next i15
b(c(16)) = 0: c(16) = 0
160 Next i16
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s1)
y = MsgBox(t10, 0, "Routine MgcSqr4e")
End
' Print results (selected numbers)
640 Cells(n9, 1).Value = a(1)
Cells(n9, 2).Value = a(2)
Cells(n9, 3).Value = a(3)
Cells(n9, 4).Value = a(4)
Cells(n9, 5).Value = a(5)
Cells(n9, 6).Value = a(6)
Cells(n9, 7).Value = a(7)
Cells(n9, 8).Value = a(8)
Cells(n9, 9).Value = a(9)
Cells(n9, 10).Value = a(10)
Cells(n9, 11).Value = a(11)
Cells(n9, 12).Value = a(12)
Cells(n9, 13).Value = a(13)
Cells(n9, 14).Value = a(14)
Cells(n9, 15).Value = a(15)
Cells(n9, 16).Value = a(16)
Return
' Print results (squares)
650 n2 = n2 + 1
If n2 = 5 Then
n2 = 1: k1 = k1 + 5: k2 = n3
Else
If n9 > 1 Then k2 = k2 + 5
End If
''Cells(k1 + 1, k2 + 1).Select
Cells(k1 + 1, k2 + 1).Value = a(1): Cells(k1 + 1, k2 + 2).Value = a(2): Cells(k1 + 1, k2 + 3).Value = a(3)
Cells(k1 + 1, k2 + 4).Value = a(4)
Cells(k1 + 2, k2 + 1).Value = a(5): Cells(k1 + 2, k2 + 2).Value = a(6): Cells(k1 + 2, k2 + 3).Value = a(7)
Cells(k1 + 2, k2 + 4).Value = a(8)
Cells(k1 + 3, k2 + 1).Value = a(9): Cells(k1 + 3, k2 + 2).Value = a(10): Cells(k1 + 3, k2 + 3).Value = a(11)
Cells(k1 + 3, k2 + 4).Value = a(12)
Cells(k1 + 4, k2 + 1).Value = a(13): Cells(k1 + 4, k2 + 2).Value = a(14): Cells(k1 + 4, k2 + 3).Value = a(15)
Cells(k1 + 4, k2 + 4).Value = a(16)
Return
' Exclude solutions with identical numbers
800 fl1 = 1
For j1 = 1 To 16
a2 = a(j1)
For j2 = (1 + j1) To 16
If a2 = a(j2) Then fl1 = 0: GoTo 850
Next j2
Next j1
850 Return
' Exclude solutions outside range i1(1) ... i1(16)
900 fl1 = 1
For j1 = b1 To b2
a2 = a(j1)
For j2 = 1 To 16
If a2 = i1(j2) Then GoTo 950
Next j2
fl1 = 0: Return
950 Next j1
Return
End Sub