' Generates (Pan) Magic Squares of order 5 based on Sudoku Comparable Squares
' Tested with Office 2007 under Windows 7
Sub CnstrSqrs5a()
Dim b(2, 25), a(25), s(20)
Sheets("Klad1").Select
y = MsgBox("Locked", vbCritical, "Routine CnstrCbs5a")
End
n4 = 240 'Sudoku51: 240
'Sudoku52: 960
'SudSqrs5: 4288
n2 = 0: n9 = 0: k1 = 1: k2 = 1
s1 = 65
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
GoSub 200: If fl1 = 0 Then GoTo 20 'Check Magic Sum
GoSub 300: If fl1 = 0 Then GoTo 20 'Ckeck identical numbers
'********************** 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 CnstrSqrs5a")
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("Sudoku51").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 20 '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
' Print results (selected numbers)
740 Cells(n9, 25).Select
For i1 = 1 To 25
Cells(n9, i1).Value = a(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 = a(i3)
Next i2
Next i1
Return
End Sub