' Generates Magic Squares of order 5, Magic Constant 425, based on Sudoku Comparable Squares
' Tested with Office 2007 under Windows 7
Sub CnstrSqrs5c()
Dim b(2, 25), a(25), a1(25), a25(25), s(20)
Sheets("Klad1").Select
y = MsgBox("Locked", vbCritical, "Routine CnstrCbs5c")
End
n4 = 1060 'Sudoku51: 240
'Sudoku52: 960
'SudSqrs5: 4288
'SudSqrs5a :1060 Like SudSqrs5 but only for a(25) = 2
n2 = 0: n9 = 0: k1 = 1: k2 = 1
s1 = 425
' Define variables
a1(1) = 11: a1(2) = 12: a1(3) = 15: a1(4) = 18: a1(5) = 19:
a1(6) = 20: a1(7) = 21: a1(8) = 24: a1(9) = 27: a1(10) = 28:
a1(11) = 81: a1(12) = 82: a1(13) = 85: a1(14) = 88: a1(15) = 89:
a1(16) = 142: a1(17) = 143: a1(18) = 146: a1(19) = 149: a1(20) = 150:
a1(21) = 151: a1(22) = 152: a1(23) = 155: a1(24) = 158: a1(25) = 159:
t1 = Timer
For j1 = 1 To n4
For j2 = 1 To n4
If j2 = j1 Then GoTo 20
j10 = j1: j20 = 1: GoSub 100 'Read Sudoku Comparable Square 1
j10 = j2: j20 = 2: GoSub 100 'Read Sudoku Comparable Square 2
For j4 = 1 To 25
a(j4) = b(1, j4) + 5 * b(2, j4) + 1
Next j4
For j4 = 1 To 25 'Transform to Defined Variables
a(j4) = a1(a(j4))
Next j4
GoSub 200: If fl1 = 0 Then GoTo 20 'Check Magic Sum
GoSub 300: If fl1 = 0 Then GoTo 20 'Check identical numbers
GoSub 700 'Reflect Square
'********************** Only Counting **************************
''Cells(2, 1).Value = j1
''Cells(2, 2).Value = j2
''Cells(2, 4).Select: Cells(2, 4).Value = Cells(2, 4).Value + 1
'***************************************************************
n9 = n9 + 1: GoSub 740 'Print results (selected numbers)
'' n9 = n9 + 1: GoSub 750 'Print results (squares)
20 Next j2
Next j1
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s1)
y = MsgBox(t10, 0, "Routine CnstrSqrs5c")
End
' Read Sudoku Comparable Squares
100 k11 = Int((j10 - 1) / 4)
k12 = j10 - k11 * 4
k21 = 1 + k11 * 6
k22 = 1 + (k12 - 1) * 6
i3 = 0
For i1 = 1 To 5
For i2 = 1 To 5
i3 = i3 + 1
b(j20, i3) = Sheets("SudSqrs5a").Cells(k21 + i1, k22 + i2).Value
Next i2
Next i1
Return
' Check Magic Sum
200 fl1 = 1
s(1) = a(1) + a(2) + a(3) + a(4) + a(5)
s(2) = a(6) + a(7) + a(8) + a(9) + a(10)
s(3) = a(11) + a(12) + a(13) + a(14) + a(15)
s(4) = a(16) + a(17) + a(18) + a(19) + a(20)
s(5) = a(21) + a(22) + a(23) + a(24) + a(25)
s(6) = a(1) + a(6) + a(11) + a(16) + a(21)
s(7) = a(2) + a(7) + a(12) + a(17) + a(22)
s(8) = a(3) + a(8) + a(13) + a(18) + a(23)
s(9) = a(4) + a(9) + a(14) + a(19) + a(24)
s(10) = a(5) + a(10) + a(15) + a(20) + a(25)
s(11) = a(1) + a(7) + a(13) + a(19) + a(25)
s(12) = a(5) + a(9) + a(13) + a(17) + a(21)
' Pan Diagonals
s(13) = a(2) + a(8) + a(14) + a(20) + a(21)
s(14) = a(3) + a(9) + a(15) + a(16) + a(22)
s(15) = a(4) + a(10) + a(11) + a(17) + a(23)
s(16) = a(5) + a(6) + a(12) + a(18) + a(24)
s(17) = a(4) + a(8) + a(12) + a(16) + a(25)
s(18) = a(3) + a(7) + a(11) + a(20) + a(24)
s(19) = a(2) + a(6) + a(15) + a(19) + a(23)
s(20) = a(1) + a(10) + a(14) + a(18) + a(22)
For i1 = 1 To 12 '12 for Magic, 20 for Pan Magic
If s(i1) <> s1 Then fl1 = 0: Return
Next i1
Return
' Check identical numbers
300 fl1 = 1
For i1 = 1 To 25
a2 = a(i1)
For i2 = (1 + i1) To 25
If a2 = a(i2) Then fl1 = 0: Return
Next i2
Next i1
Return
' Reflect Square (85 in left corner)
700
i2 = -4
For i1 = 1 To 5
i2 = i2 + 5
a25(i2) = a(i2 + 4): a25(i2 + 4) = a(i2)
a25(i2 + 1) = a(i2 + 3): a25(i2 + 3) = a(i2 + 1)
a25(i2 + 2) = a(i2 + 2)
Next i1
Return
' Print results (selected numbers)
740 Cells(n9, 25).Select
GoSub 700
For i1 = 1 To 25
Cells(n9, i1).Value = a25(i1)
Next i1
Cells(n9, 27).Value = j1
Cells(n9, 28).Value = j2
Return
' Print results (squares)
750 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 = a25(i3)
Next i2
Next i1
Return
End Sub