Vorige Pagina Volgende Pagina About the Author

' Constructs 11 x 11 Bordered Magic Squares (Part 1)
' - Reads One Symmetric Magic Center Square Order 5
' - Generates Anti Symmetric Semi Magic Corner Squares (3 x 3) for Composed Border (11 x 11)

' Tested with Office 2007 under Windows 7

Sub Priem11d1()

    Dim a1(121), a11(121), a(25), b1(121), b(121), c(25), a2(9), b2(121), c2(9)

y = MsgBox("Locked", vbCritical, "Routine Priem11d1")
End

    n2 = 0: n3 = 0: k1 = 1: k2 = 1: n9 = 0: n10 = 0
    ShtNm2 = "Solutions5"

'   Generate Squares

    Sheets("Klad1").Select
    
    t1 = Timer

        Pr3 = 122:  Cntr3 = 61: nVar = 121
        
        s1 = 3 * Cntr3                                   'MC3
        s5 = 5 * Cntr3                                   'MC5
        s11 = 11 * Cntr3                                 'MC11

For j100 = 244 To 1065
    
'       Redefine Integers

        Erase b1
        For i1 = 1 To nVar
            b1(i1) = i1
        Next i1
        pMax = 121

'       Read Symmetric Magic Square (5 x 5)
        
        For i1 = 1 To 25
            a(i1) = Sheets(ShtNm2).Cells(j100, i1).Value
        Next i1
        n31 = 1: n32 = 25: GoSub 900 'Remove used integers from available integers

        Erase a11

'       Store in a11()

            a11(37) = a(1):  a11(38) = a(2):  a11(39) = a(3):  a11(40) = a(4):  a11(41) = a(5):
            a11(48) = a(6):  a11(49) = a(7):  a11(50) = a(8):  a11(51) = a(9):  a11(52) = a(10):
            a11(59) = a(11): a11(60) = a(12): a11(61) = a(13): a11(62) = a(14): a11(63) = a(15):
            a11(70) = a(16): a11(71) = a(17): a11(72) = a(18): a11(73) = a(19): a11(74) = a(20):
            a11(81) = a(21): a11(82) = a(22): a11(83) = a(23): a11(84) = a(24): a11(85) = a(25):

        Erase a

'       Restore available pairs in a1()

        n10 = 0
        For j1 = 1 To pMax
            If b1(j1) <> 0 Then
                n10 = n10 + 1
                a1(n10) = b1(j1)
            End If
        Next j1
        m1 = 1: m2 = n10: n10 = 0
 
        GoSub 2000                             'Determine 4 Corner Squares
     
     n10 = 0
1000 Next j100

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

End

'    Determine 4 Corner Squares

2000 Erase b2, c2
    
    For jj9 = m1 To m2                                                     'a2(9)
    If b1(a1(jj9)) = 0 Then GoTo 165
    If b2(a1(jj9)) = 0 Then b2(a1(jj9)) = a1(jj9): c2(9) = a1(jj9) Else GoTo 165
    a2(9) = a1(jj9)
    
    For jj8 = m1 To m2                                                     'a2(8)
    If b1(a1(jj8)) = 0 Then GoTo 125
    If b2(a1(jj8)) = 0 Then b2(a1(jj8)) = a1(jj8): c2(8) = a1(jj8) Else GoTo 125
    a2(8) = a1(jj8)
    
        a2(7) = s1 - a2(8) - a2(9):
        If a2(7) < a1(m1) Or a2(7) > a1(m2) Then GoTo 115:
        If b1(a2(7)) = 0 Then GoTo 115
        
    For jj6 = m1 To m2                                                     'a2(6)
    If b1(a1(jj6)) = 0 Then GoTo 100
    If b2(a1(jj6)) = 0 Then b2(a1(jj6)) = a1(jj6): c2(6) = a1(jj6) Else GoTo 100
    a2(6) = a1(jj6)
    
        a2(5) = -s1 + a2(6) + a2(8) + 2 * a2(9)
        If a2(5) < a1(m1) Or a2(5) > a1(m2) Then GoTo 80:
        If b1(a2(5)) = 0 Then GoTo 80
        
        a2(4) = 2 * s1 - 2 * a2(6) - a2(8) - 2 * a2(9)
        If a2(4) < a1(m1) Or a2(4) > a1(m2) Then GoTo 80:
        If b1(a2(4)) = 0 Then GoTo 80
        
        a2(3) = s1 - a2(6) - a2(9)
        If a2(3) < a1(m1) Or a2(3) > a1(m2) Then GoTo 80:
        If b1(a2(3)) = 0 Then GoTo 80
        
        a2(2) = 2 * s1 - a2(6) - 2 * a2(8) - 2 * a2(9)
        If a2(2) < a1(m1) Or a2(2) > a1(m2) Then GoTo 80:
        If b1(a2(2)) = 0 Then GoTo 80
        
        a2(1) = -2 * s1 + 2 * a2(6) + 2 * a2(8) + 3 * a2(9)
        If a2(1) < a1(m1) Or a2(1) > a1(m2) Then GoTo 80:
        If b1(a2(1)) = 0 Then GoTo 80
    
    '                 Exclude solutions with identical numbers a2()
    
                      GoSub 810: If fl1 = 0 Then GoTo 80
                      GoSub 1900: If fl1 = 0 Then GoTo 80           'Anti Symmetric
                              
                      n10 = n10 + 1
                             
                      If n10 < 2 Then
                             GoSub 750                              'Transform and Assign Corner Squares
                             GoSub 910                              'Remove used integers a2() from available integers b1()
                             Erase b2, c2: GoTo 165
                      Else
                             GoSub 750                              'Transform and Assign Corner Squares
                             GoSub 850                              'Double Check Identical Integers
                             If fl1 = 1 Then
'                                n9 = n9 + 1: GoSub 650             'Print Composed Squares
                                 n9 = n9 + 1: GoSub 640             'Print Composed lines
                             End If
                      End If
                      If n10 = 2 Then Erase b2, c2: Return          'Only four squares required
       
80      b2(c2(6)) = 0: c2(6) = 0
100     Next jj6
       
115     b2(c2(8)) = 0: c2(8) = 0
125     Next jj8
        
        b2(c2(9)) = 0: c2(9) = 0
165     Next jj9

        Return

'   Print results (lines)

640  Cells(n9, 123).Select
     For i1 = 1 To 121
         Cells(n9, i1).Value = a11(i1)
     Next i1
     Cells(n9, 122).Value = s11
     Cells(n9, 123).Value = j100
     Return

'    Print results (squares)

650  n2 = n2 + 1
     If n2 = 2 Then
         n2 = 1: k1 = k1 + 12: k2 = 1
     Else
         If n9 > 1 Then k2 = k2 + 12
     End If

     Cells(k1, k2 + 1).Select
     Cells(k1, k2 + 1).Font.Color = -4165632
     Cells(k1, k2 + 1).Value = "MC = " + CStr(s11)
    
     i3 = 0
     For i1 = 1 To 11
         For i2 = 1 To 11
             i3 = i3 + 1
             Cells(k1 + i1, k2 + i2).Value = a11(i3)
         Next i2
     Next i1
     Return

'    Transform and Assign Corner and Border Squares

750  Select Case n10

        Case 1: 'Left Top
                
                a11(1) = a2(3):  a11(2) = a2(2):   a11(3) = a2(1):
                a11(12) = a2(6): a11(13) = a2(5):  a11(14) = a2(4):
                a11(23) = a2(9): a11(24) = a2(8):  a11(25) = a2(7):
                
                'Right Bottom
        
                a11(97) = Pr3 - a11(25):  a11(98) = Pr3 - a11(24):  a11(99) = Pr3 - a11(23):
                a11(108) = Pr3 - a11(14): a11(109) = Pr3 - a11(13): a11(110) = Pr3 - a11(12):
                a11(119) = Pr3 - a11(3):  a11(120) = Pr3 - a11(2):  a11(121) = Pr3 - a11(1):
                
        Case 2: 'Right Top
        
                a11(9) = a2(1):   a11(10) = a2(2):   a11(11) = a2(3):
                a11(20) = a2(4):  a11(21) = a2(5):  a11(22) = a2(6):
                a11(31) = a2(7):  a11(32) = a2(8):  a11(33) = a2(9):

                'Left Bottom
        
                a11(89) = Pr3 - a11(33):  a11(90) = Pr3 - a11(32):  a11(91) = Pr3 - a11(31):
                a11(100) = Pr3 - a11(22): a11(101) = Pr3 - a11(21): a11(102) = Pr3 - a11(20):
                a11(111) = Pr3 - a11(11): a11(112) = Pr3 - a11(10): a11(113) = Pr3 - a11(9):
                
     End Select
     
     Return

'    Exclude solutions with identical numbers a2()

810  fl1 = 1
     For j1 = 1 To 9
        a20 = a2(j1)
        For j2 = (1 + j1) To 9
            If a20 = a2(j2) Then fl1 = 0: Return
        Next j2
     Next j1
     Return

'    Exclude solutions with identical numbers a11()

850  fl1 = 1
     For j1 = 1 To 121
        a20 = a11(j1): If a20 = 0 Then GoTo 855
        For j2 = (1 + j1) To 121
            If a20 = a11(j2) Then fl1 = 0: Return
        Next j2
855  Next j1
     Return

'    Remove used integers a() from available integers b1()

900  For i1 = n31 To n32
         b1(a(i1)) = 0
     Next i1
     Return

'    Remove used integers a2() and complements from available integers b1()

910  For i1 = 1 To 9
         b1(a2(i1)) = 0: b1(Pr3 - a2(i1)) = 0
     Next i1
     Return
    
'   Check Pairs

1900 fl1 = 1: n25 = 0
     For j1 = 1 To 9
        a20 = Pr3 - a2(j1)          'Complement
        For j2 = (1 + j1) To 9
             If a20 = a2(j2) Then fl1 = 0: Return
         Next j2
     Next j1
     Return

End Sub

Vorige Pagina Volgende Pagina About the Author