Vorige Pagina About the Author

' Constructs Simple Magic Squares of order 12, composed of
' 8 Semi Magic Anti Symmetric Diagonal Squares and 8 Semi Magic Anti Symmetric Border Squares

' Tested with Office 2007 under Windows 7

Sub Priem3f2()

Dim a1(1200), b1(21803), a(9), b(21803), c(9), a12(144)
Dim a2(9), b2(21803), c2(9)

y = MsgBox("Locked", vbCritical, "Routine Priem3f2")
End
    
    n1 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
    ShtNm1 = "Pairs6"                           'Pairs6, Pairs8 for 6 x 6, 12 x 12, 18 x 18
                                                'Pairs3, Pairs7 for 9 x 9, 15 x 15
    Sheets("Klad1").Select
    t1 = Timer

For j100 = 1427 To 8388

    GoSub 3010                                  'Read Prime Numbers From Sheet ShtNm1
    If nVar < 144 Then GoTo 1000
    If nSemi3 <> 6 Then GoTo 1000

'   Generate Diagonal Squares

    For jj9 = m1 To m2 ''m1 To m2                                          'a(9)
    If b1(a1(jj9)) = 0 Then GoTo 1090
    If b(a1(jj9)) = 0 Then b(a1(jj9)) = a1(jj9): c(9) = a1(jj9) Else GoTo 1090
    a(9) = a1(jj9)
   
    For jj8 = m1 To m2                                                     'a(8)
    If b1(a1(jj8)) = 0 Then GoTo 1080
    If b(a1(jj8)) = 0 Then b(a1(jj8)) = a1(jj8): c(8) = a1(jj8) Else GoTo 1080
    a(8) = a1(jj8)
  
        a(7) = s1 - a(8) - a(9):
        If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 1070:
        If b1(a(7)) = 0 Then GoTo 1070
        If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 1070
        
    For jj6 = m1 To m2                                                     'a(6)
    If b1(a1(jj6)) = 0 Then GoTo 1060
    If b(a1(jj6)) = 0 Then b(a1(jj6)) = a1(jj6): c(6) = a1(jj6) Else GoTo 1060
    a(6) = a1(jj6)
       
        a(5) = -s1 + a(6) + a(8) + 2 * a(9)
        If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 1050:
        If b1(a(5)) = 0 Then GoTo 1050
        If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 1050
        
        a(4) = 2 * s1 - 2 * a(6) - a(8) - 2 * a(9)
        If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 1040:
        If b1(a(4)) = 0 Then GoTo 1040
        If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 1040
        
        a(3) = s1 - a(6) - a(9)
        If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 1030:
        If b1(a(3)) = 0 Then GoTo 1030
        If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 1030
        
        a(2) = 2 * s1 - a(6) - 2 * a(8) - 2 * a(9)
        If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 1020:
        If b1(a(2)) = 0 Then GoTo 1020
        If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 1020
        
        a(1) = -2 * s1 + 2 * a(6) + 2 * a(8) + 3 * a(9)
        If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 1010:
        If b1(a(1)) = 0 Then GoTo 1010
        If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 1010
        
                          GoSub 950: If fl1 = 0 Then GoTo 1005  ' Anti Symmetric

                          n10 = n10 + 1                          
                          If n10 = 4 Then
                             GoSub 750                          ' Load Square Nr 4
                             GoSub 900                          ' Remove used primes from available primes
                             GoSub 2000                         ' Generate Border Squares
                             Erase b, c: GoTo 1000              ' Next Magic Sum
                          Else
                             GoSub 750                          ' Load Square Nr 1,2 or 3
                             GoSub 900                          ' Remove used primes from available primes
                             Erase b, c: GoTo 1090
                          End If

1005    b(c(1)) = 0: c(1) = 0
1010    b(c(2)) = 0: c(2) = 0
1020    b(c(3)) = 0: c(3) = 0
1030    b(c(4)) = 0: c(4) = 0
1040    b(c(5)) = 0: c(5) = 0
1050    b(c(6)) = 0: c(6) = 0
1060    Next jj6
        
        b(c(7)) = 0: c(7) = 0
1070    b(c(8)) = 0: c(8) = 0
1080    Next jj8
        
        b(c(9)) = 0: c(9) = 0
1090    Next jj9

1000  n10 = 0
      Next j100

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

End

'   Generate Border Squares

2000 Erase b2, c2
    
'    Determine Border Square

For jjj9 = m1 To m2                                                     'a2(9)
If b1(a1(jjj9)) = 0 Then GoTo 2090
If b2(a1(jjj9)) = 0 Then b2(a1(jjj9)) = a1(jjj9): c2(9) = a1(jjj9) Else GoTo 2090
a2(9) = a1(jjj9)

For jjj8 = m1 To m2                                                     'a2(8)
If b1(a1(jjj8)) = 0 Then GoTo 2080
If b2(a1(jjj8)) = 0 Then b2(a1(jjj8)) = a1(jjj8): c2(8) = a1(jjj8) Else GoTo 2080
a2(8) = a1(jjj8)

    a2(7) = s1 - a2(8) - a2(9):
    If a2(7) < a1(m1) Or a2(7) > a1(m2) Then GoTo 2070:
    If b1(a2(7)) = 0 Then GoTo 2070
    If b2(a2(7)) = 0 Then b2(a2(7)) = a2(7): c2(7) = a2(7) Else GoTo 2070
    
For jjj6 = m1 To m2                                                     'a2(6)
If b1(a1(jjj6)) = 0 Then GoTo 2060
If b2(a1(jjj6)) = 0 Then b2(a1(jjj6)) = a1(jjj6): c2(6) = a1(jjj6) Else GoTo 2060
a2(6) = a1(jjj6)

    a2(3) = s1 - a2(6) - a2(9)
    If a2(3) < a1(m1) Or a2(3) > a1(m2) Then GoTo 2030:
    If b1(a2(3)) = 0 Then GoTo 2030
    If b2(a2(3)) = 0 Then b2(a2(3)) = a2(3): c2(3) = a2(3) Else GoTo 2030

For jjj5 = m1 To m2                                                     'a2(5)
If b1(a1(jjj5)) = 0 Then GoTo 2050
If b2(a1(jjj5)) = 0 Then b2(a1(jjj5)) = a1(jjj5): c2(5) = a1(jjj5) Else GoTo 2050
a2(5) = a1(jjj5)
    
    a2(4) = s1 - a2(5) - a2(6)
    If a2(4) < a1(m1) Or a2(4) > a1(m2) Then GoTo 2040:
    If b1(a2(4)) = 0 Then GoTo 2040
    If b2(a2(4)) = 0 Then b2(a2(4)) = a2(4): c2(4) = a2(4) Else GoTo 2040
    
    a2(2) = s1 - a2(5) - a2(8)
    If a2(2) < a1(m1) Or a2(2) > a1(m2) Then GoTo 2020:
    If b1(a2(2)) = 0 Then GoTo 2020
    If b2(a2(2)) = 0 Then b2(a2(2)) = a2(2): c2(2) = a2(2) Else GoTo 2020
    
    a2(1) = -s1 + a2(5) + a2(6) + a2(8) + a2(9)
    If a2(1) < a1(m1) Or a2(1) > a1(m2) Then GoTo 2010:
    If b1(a2(1)) = 0 Then GoTo 2010
    If b2(a2(1)) = 0 Then b2(a2(1)) = a2(1): c2(1) = a2(1) Else GoTo 2010

                      GoSub 950: If fl1 = 0 Then GoTo 2005    ' Anti Symmetric

                      n10 = n10 + 1
                      If n10 < 8 Then
                             GoSub 750                        'Transform and Assign Border Squares
                             GoSub 910                        'Remove used primes a2() from available primes b1()
                             Erase b2, c2: GoTo 2090
                      Else
                             GoSub 750                        'Transform and Assign Border Squares
                             GoSub 850                        'Double Check Identical Integers
                             If fl1 = 1 Then
                                    n9 = n9 + 1: GoSub 650    'Print Composed Squares
                             End If
                      End If
                      If n10 = 8 Then Erase b2, c2: Return    'Eight squares required

2005  b2(c2(1)) = 0: c2(1) = 0
2010  b2(c2(2)) = 0: c2(2) = 0
2020  b2(c2(4)) = 0: c2(4) = 0
2040  b2(c2(5)) = 0: c2(5) = 0
2050  Next jjj5
   
     b2(c2(3)) = 0: c2(3) = 0
2030 b2(c2(6)) = 0: c2(6) = 0
2060 Next jjj6

     b2(c2(7)) = 0: c2(7) = 0
2070 b2(c2(8)) = 0: c2(8) = 0
2080 Next jjj8
    
     b2(c2(9)) = 0: c2(9) = 0
2090 Next jjj9

     Return

'   Print results (squares 12 x 12)

650  n2 = n2 + 1
     If n2 = 2 Then
         n2 = 1: k1 = k1 + 13: k2 = 1
     Else
         If n9 > 1 Then k2 = k2 + 13
     End If
     
     Cells(k1, k2 + 1).Select
     Cells(k1, k2 + 1).Font.Color = -4165632
     Cells(k1, k2 + 1).Value = "MC = " + CStr(s2)
    
     i3 = 0
     For i1 = 1 To 12
         For i2 = 1 To 12
             i3 = i3 + 1
             Cells(k1 + i1, k2 + i2).Value = a12(i3)
         Next i2
     Next i1
    
     Return

'    Transform and Assign Sub Squares

750  Select Case n10

    'Diagonal Squares, Outer/Inner

        Case 1:
        a12(1) = a(3):  a12(2) = a(2):  a12(3) = a(1):
        a12(13) = a(6): a12(14) = a(5): a12(15) = a(4):
        a12(25) = a(9): a12(26) = a(8): a12(27) = a(7):
        
        Case 2:
        a12(10) = a(1): a12(11) = a(2): a12(12) = a(3):
        a12(22) = a(4): a12(23) = a(5): a12(24) = a(6):
        a12(34) = a(7): a12(35) = a(8): a12(36) = a(9):
       
        Case 3:
        a12(40) = a(3): a12(41) = a(2): a12(42) = a(1):
        a12(52) = a(6): a12(53) = a(5): a12(54) = a(4):
        a12(64) = a(9): a12(65) = a(8): a12(66) = a(7):
        
        Case 4:
        a12(43) = a(1): a12(44) = a(2): a12(45) = a(3):
        a12(55) = a(4): a12(56) = a(5): a12(57) = a(6):
        a12(67) = a(7): a12(68) = a(8): a12(69) = a(9):
 
    'Border Squares, Outer/Inner

        Case 5:
        a12(4) = a2(1):  a12(5) = a2(2):  a12(6) = a2(3):
        a12(16) = a2(4): a12(17) = a2(5): a12(18) = a2(6):
        a12(28) = a2(7): a12(29) = a2(8): a12(30) = a2(9):
        
        Case 6:
        a12(7) = a2(1):  a12(8) = a2(2):  a12(9) = a2(3):
        a12(19) = a2(4): a12(20) = a2(5): a12(21) = a2(6):
        a12(31) = a2(7): a12(32) = a2(8): a12(33) = a2(9):

        Case 7:
        a12(46) = a2(1): a12(47) = a2(2): a12(48) = a2(3):
        a12(58) = a2(4): a12(59) = a2(5): a12(60) = a2(6):
        a12(70) = a2(7): a12(71) = a2(8): a12(72) = a2(9):
        
        Case 8:
        a12(37) = a2(1): a12(38) = a2(2): a12(39) = a2(3):
        a12(49) = a2(4): a12(50) = a2(5): a12(51) = a2(6):
        a12(61) = a2(7): a12(62) = a2(8): a12(63) = a2(9):

'       Assign Associated Elements

        For i1 = 1 To 72
                a12(145 - i1) = Pr3 - a12(i1)
        Next i1

     End Select

     Return

'    Exclude solutions with identical numbers a12()

850  fl1 = 1
     For j1 = 1 To 144
        a20 = a12(j1)
        For j2 = (1 + j1) To 144
            If a20 = a12(j2) Then fl1 = 0: Return
        Next j2
     Next j1
     Return

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

900 For i1 = 1 To 9
        b1(a(i1)) = 0: b1(Pr3 - a(i1)) = 0
    Next i1
    Return
     
'    Remove used primes a2() from available primes b1()

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

950 fl1 = 1: n25 = 0
    For j1 = 1 To 9
       a20 = Pr3 - a(j1)          'Complement
       For j2 = (1 + j1) To 9
            If a20 = a(j2) Then fl1 = 0: Return
       Next j2
    Next j1
    Return
     
'    Read Prime Numbers From Sheet ShtNm1

3010 Pr3 = Sheets(ShtNm1).Cells(j100, 1).Value    'Pair Sum
     s1 = 3 * Pr3 / 2                             'MC3
     s2 = 6 * Pr3                                 'MC12
     nVar = Sheets(ShtNm1).Cells(j100, 5).Value
     
     nSemi3 = Sheets(ShtNm1).Cells(j100, 6).Value 'Expected Nmbr Semi Magic Squares
    
     m1 = 1: m2 = nVar
    
     For i1 = m1 To m2
         a1(i1) = Sheets(ShtNm1).Cells(j100, i1 + 10).Value
     Next i1
     If a1(1) = 1 Then m1 = 2
    
     Erase b1
     For i1 = m1 To m2
         b1(a1(i1)) = a1(i1)
     Next i1
   
     Return
     
End Sub

Vorige Pagina About the Author