Vorige Pagina About the Author

' Generates 3 x 3 Semi Magic Squares of Squares, Six Magic Lines

' Tested with Office 2007 under Windows 7

Sub SqrSqr3a()

Dim a(9)

y = MsgBox("Locked", vbCritical, "Routine SqrsSqrs3a")
End
    
    n1 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
    m1 = 0: m2 = 160    
    m3 = m1 ^ 2: m4 = m2 ^2
    
    sMin = 95: sMax = 73028
 
    Sheets("Klad1").Select
    
    t1 = Timer
   
For j100 = sMin To sMax         'Magic sum

n10 = 0: s1 = j100
Cells(k1 + 1, 1).Value = j100

For j9 = m1 To m2                                                     'a(9)
a(9) = j9 ^ 2

For j8 = m1 To m2                                                     'a(8)
If j8 = j9 Then GoTo 80
a(8) = j8 ^ 2

         a(7) = s1 - a(8) - a(9)
         If a(7) < m3 Or a(7) > m4 Then GoTo 80
         a2 = Sqr(a(7)): If Int(a2) <> a2 Then GoTo 80
         If a(7) = a(8) Or a(7) = a(9) Then GoTo 80

For j6 = m1 To m2                                                     'a(6)
If j6 = j8 Or j6 = j9 Then GoTo 60
a(6) = j6 ^ 2
    
For j5 = m1 To m2                                                     'a(5)
If j5 = j6 Or j5 = j8 Or j5 = j9 Then GoTo 50
a(5) = j5 ^ 2

        a(4) = s1 - a(5) - a(6)
        If a(4) < m3 Or a(4) > m4 Then GoTo 50
        a2 = Sqr(a(4)): If Int(a2) <> a2 Then GoTo 50
        a(3) = -a(6) + a(7) + a(8)
        If a(3) < m3 Or a(3) > m4 Then GoTo 50
        a2 = Sqr(a(3)): If Int(a2) <> a2 Then GoTo 50
        a(2) = s1 - a(5) - a(8)
        If a(2) < m3 Or a(2) > m4 Then GoTo 50
        a2 = Sqr(a(2)): If Int(a2) <> a2 Then GoTo 50
        a(1) = a(5) + a(6) - a(7)
        If a(1) < m3 Or a(1) > m4 Then GoTo 50
        a2 = Sqr(a(1)): If Int(a2) <> a2 Then GoTo 50
    
'       Exclude solutions with identical numbers or out of range
    
        GoSub 800: If fl1 = 0 Then GoTo 50
     
'       n9 = n9 + 1: GoSub 640 'Print results (selected numbers)
        n10 = n10 + 1
        n9 = n9 + 1: GoSub 650 'Print results (squares)
        
50 Next j5
60 Next j6
80 Next j8
90 Next j9

Next j100

   t2 = Timer
    
   t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
   y = MsgBox(t10, 0, "Routine SqrsSqrs3a")

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 = 7 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

End Sub

Vorige Pagina About the Author