' Searches for 3 x 3 Semi Magic Squares of Squares, Eight Magic Lines, Squared Entries >= 7
' Tested with Office 2007 under Windows 7
Sub SqrSqr3c()
Dim a(9), a1(100)
y = MsgBox("Locked", vbCritical, "Routine SqrSqr3c")
End
n1 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
m1 = 1: m3 = 0
Sheets("Klad1").Select
t1 = Timer
For j100 = 2 To 1111 '( 26 = Test Case)
n10 = 0:
s1 = Sheets("Pairs3").Cells(j100, 3).Value
m2 = Sheets("Pairs3").Cells(j100, 5).Value
For j200 = 1 To m2
a1(j200) = Sheets("Pairs3").Cells(j100, j200 + 6).Value
Next j200
For j9 = m1 To m2 'a(9)
a(9) = a1(j9)
For j8 = m1 To m2 'a(8)
If j8 = j9 Then GoTo 80
a(8) = a1(j8)
a(7) = s1 - a(8) - a(9)
If a(7) < m3 Then GoTo 80
a2 = Sqr(a(7)): If Int(a2) <> a2 Then GoTo 80
a(6) = 4 * s1 / 3 - a(8) - 2 * a(9)
If a(6) < m3 Then GoTo 80
a2 = Sqr(a(6)): If Int(a2) <> a2 Then GoTo 80
a(5) = s1 / 3
If a(5) < m3 Then GoTo 80
a2 = Sqr(a(5)): If Int(a2) <> a2 Then GoTo 80
a(4) = -2 * s1 / 3 + a(8) + 2 * a(9)
If a(4) < m3 Then GoTo 80
''a2 = Sqr(a(4)): If Int(a2) <> a2 Then GoTo 80 'Base Case: Disabled
a(3) = -s1 / 3 + a(8) + a(9)
If a(3) < m3 Then GoTo 80
''a2 = Sqr(a(3)): If Int(a2) <> a2 Then GoTo 80 'Base Case: Disabled
a(2) = 2 * s1 / 3 - a(8)
If a(2) < m3 Then GoTo 80
a2 = Sqr(a(2)): If Int(a2) <> a2 Then GoTo 80
a(1) = 2 * s1 / 3 - a(9)
If a(1) < m3 Then GoTo 80
a2 = Sqr(a(1)): If Int(a2) <> a2 Then GoTo 80
' Exclude solutions with identical numbers
GoSub 800: If fl1 = 0 Then GoTo 80
' GoSub 850: If n40 > 2 Then GoTo 80
' n9 = n9 + 1: GoSub 640 'Print results (selected numbers)
n10 = n10 + 1
n9 = n9 + 1: GoSub 650 'Print results (squares)
80 Next j8
90 Next j9
100 Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine SqrSqr3c")
End
' Print results (selected numbers)
640 Cells(n9, 9).Select
For i1 = 1 To 9
Cells(n9, i1).Value = a(i1)
Next i1
Return
' Print results (squares)
650 n1 = n1 + 1
If n1 = 5 Then
n1 = 1: k1 = k1 + 4: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 4
End If
Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
If n10 = 1 Then
Cells(k1, k2 + 1).Value = CStr(n10) + " (MC = " + CStr(s1) + " )"
Else
Cells(k1, k2 + 1).Value = CStr(n10)
End If
i3 = 0
For i1 = 1 To 3
For i2 = 1 To 3
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 9
a2 = a(j1)
For j2 = (1 + j1) To 9
If a2 = a(j2) Then fl1 = 0: Return
Next j2
Next j1
Return
' Count squared variables
850 n40 = 0
For i1 = 1 To 9
a2 = Sqr(a(i1)): If Int(a2) <> a2 Then n40 = n40 + 1
Next i1
''y = MsgBox("n40 = " + CStr(n40), 0, "Test")
Return
End Sub