Vorige Pagina About the Author

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

' Tested with Office 2007 under Windows 7

Sub SqrSqr3b()

Dim a(9)

y = MsgBox("Locked", vbCritical, "Routine SqrSqr3b")
End
    
    n1 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
    m1 = 1: m2 = 255
    m3 = m1 ^ 2: m4 = m2 ^ 2
    
    sMin = 21609: sMax = 86436
  
    Sheets("Klad1").Select
    
    t1 = Timer
   
For j100 = sMin To sMax

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

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
        If a(7) = a(8) Or a(7) = a(9) Then GoTo 80
        a2 = Sqr(a(7)): If Int(a2) <> a2 Then GoTo 80

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

60 Next j6
80 Next j8
90 Next j9

100 Next j100

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

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