Vorige Pagina About the Author

' Constructs 7 x 7 Primme Number Associated Magic Squares composed of
' - 2 each Overlapping Magic Anti Symmetric Corner Squares of Order 4
' - 2 each Semi Magic Anti Symmetric Corner Squares of Order 3

' Tested with Office 2007 under Windows 7

Sub Priem3g()

Dim a1(653), a(16), b1(45000), b(45123), c(16), a7(49)

y = MsgBox("Locked", vbCritical, "Routine Priem3g")
End
    
    n1 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
    ShtNm1 = "Pairs7"

    Sheets("Klad1").Select
    t1 = Timer

'   Generate Squares

    For j100 = 2 To 129

'   Read Anti Symmetric Square Order 4

    For i1 = 1 To 16
        a(i1) = Sheets("Solutions4").Cells(j100, i1).Value
    Next i1
    Pr7 = 2 * a(16)                                    'Pair Sum
    MC7 = 7 * a(16)
    Rcrd1 = Sheets("Solutions4").Cells(j100, 18).Value 'Record Nr in "Pairs7"
    
    n10 = 1: GoSub 750                                 'Transform and Assign Sub Squares Order 4
    Erase a

'   Read Prime Numbers From Sheet ShtNm1

    s1 = 3 * Pr7 / 2                                   'MC3
    Cntr7 = Sheets(ShtNm1).Cells(Rcrd1, 6).Value
    
    If Cntr7 <> a7(25) Then
        y = MsgBox("Conflict in Data", vbCritical, "Read Prime Numbers")
        End
    End If
    
    nVar1 = Sheets(ShtNm1).Cells(Rcrd1, 9).Value
    
'   Read Prime Numbers From sheet ShtNm1
    
    For i1 = 1 To nVar1
        a1(i1) = Sheets(ShtNm1).Cells(Rcrd1, 9 + i1).Value
    Next i1
    m1 = 1: m2 = nVar1
    If a1(1) = 1 Then m1 = 2: m2 = nVar1 - 1

    Erase b1
    For i1 = m1 To m2
        b1(a1(i1)) = a1(i1)
    Next i1

'   Remove Primes used for Overlapping Sub Squares (4 x 4)
    
    For i1 = 1 To 49
        b1(a7(i1)) = 0
    Next i1

'   Search Anti Symmetric Square Order 3

For j9 = m1 To m2                                                     'a(9)
If b1(a1(j9)) = 0 Then GoTo 160
If b(a1(j9)) = 0 Then b(a1(j9)) = a1(j9): c(9) = a1(j9) Else GoTo 160
a(9) = a1(j9)

For j8 = m1 To m2                                                     'a(8)
If b1(a1(j8)) = 0 Then GoTo 120
If b(a1(j8)) = 0 Then b(a1(j8)) = a1(j8): c(8) = a1(j8) Else GoTo 120
a(8) = a1(j8)

    a(7) = s1 - a(8) - a(9):
    If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 110:
    If b1(a(7)) = 0 Then GoTo 110
    
For j6 = m1 To m2                                                     'a(6)
If b1(a1(j6)) = 0 Then GoTo 100
If b(a1(j6)) = 0 Then b(a1(j6)) = a1(j6): c(6) = a1(j6) Else GoTo 100
a(6) = a1(j6)

    a(5) = -s1 + a(6) + a(8) + 2 * a(9)
    If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 80:
    If b1(a(5)) = 0 Then GoTo 80
    
    a(4) = 2 * s1 - 2 * a(6) - a(8) - 2 * a(9)
    If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 80:
    If b1(a(4)) = 0 Then GoTo 80
    
    a(3) = s1 - a(6) - a(9)
    If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 80:
    If b1(a(3)) = 0 Then GoTo 80
    
    a(2) = 2 * s1 - a(6) - 2 * a(8) - 2 * a(9)
    If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 80:
    If b1(a(2)) = 0 Then GoTo 80
    
    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 80:
    If b1(a(1)) = 0 Then GoTo 80

                    GoSub 800: If fl1 = 0 Then GoTo 80 ' Check Identical Numbers
                    GoSub 950: If fl1 = 0 Then GoTo 80 ' Check Pairs
                          
                    n10 = 2
                    GoSub 750                      ' Load Square Nr 2
                    GoSub 850                      ' Back Check identical numbers a7()
                    If fl1 = 1 Then
                         n9 = n9 + 1: GoSub 650    ' Print results (squares 7 x 7)
                    End If
                    Erase b, c: n10 = 0: GoTo 10   ' Print only first square
   
80  b(c(6)) = 0: c(6) = 0
100 Next j6
   
110 b(c(8)) = 0: c(8) = 0
120 Next j8
    
    b(c(9)) = 0: c(9) = 0
160 Next j9

    Erase b, c
10  n10 = 0
    Next j100

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

End
     
'   Print results (Squares 7 x 7)

650  n2 = n2 + 1
     If n2 = 5 Then
         n2 = 1: k1 = k1 + 8: k2 = 1
     Else
         If n9 > 1 Then k2 = k2 + 8
     End If
     
     Cells(k1, k2 + 1).Select
     Cells(k1, k2 + 1).Font.Color = -4165632
     Cells(k1, k2 + 1).Value = "MC = " + CStr(MC7)
    
     i3 = 0
     For i1 = 1 To 7
         For i2 = 1 To 7
             i3 = i3 + 1
             Cells(k1 + i1, k2 + i2).Value = a7(i3)
         Next i2
     Next i1
    
     Return

'    Transform and Assign Sub Squares

750  Select Case n10

     Case 1: 'Left  Top     (4 x 4)

              a7(1) = a(1):   a7(2) = a(2):   a7(3) = a(3):   a7(4) = a(4):
              a7(8) = a(5):   a7(9) = a(6):   a7(10) = a(7):  a7(11) = a(8):
              a7(15) = a(9):  a7(16) = a(10): a7(17) = a(11): a7(18) = a(12):
              a7(22) = a(13): a7(23) = a(14): a7(24) = a(15): a7(25) = a(16):

             'Right Bottom  (4 x 4)
     
                                     a7(26) = Pr7 - a7(24): a7(27) = Pr7 - a7(23): a7(28) = Pr7 - a7(22):
              a7(32) = Pr7 - a7(18): a7(33) = Pr7 - a7(17): a7(34) = Pr7 - a7(16): a7(35) = Pr7 - a7(15):
              a7(39) = Pr7 - a7(11): a7(40) = Pr7 - a7(10): a7(41) = Pr7 - a7(9):  a7(42) = Pr7 - a7(8):
              a7(46) = Pr7 - a7(4):  a7(47) = Pr7 - a7(3):  a7(48) = Pr7 - a7(2):  a7(49) = Pr7 - a7(1):
     
     Case 2: 'Right  Top    (3 x 3)

              a7(5) = a(1):   a7(6) = a(2):   a7(7) = a(3):
              a7(12) = a(4):  a7(13) = a(5):  a7(14) = a(6):
              a7(19) = a(7):  a7(20) = a(8):  a7(21) = a(9):

             'Left Bottom   (3 x 3)
        
              a7(29) = Pr7 - a7(21): a7(30) = Pr7 - a7(20): a7(31) = Pr7 - a7(19):
              a7(36) = Pr7 - a7(14): a7(37) = Pr7 - a7(13): a7(38) = Pr7 - a7(12):
              a7(43) = Pr7 - a7(7):  a7(44) = Pr7 - a7(6):  a7(45) = Pr7 - a7(5):
        
     End Select
     
     Return
     
'   Exclude solutions with identical numbers a()

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

'   Back Check identical numbers a7()

850 fl1 = 1
    For j1 = 1 To 49
       a2 = a7(j1)
       For j2 = (1 + j1) To 49
           If a2 = a7(j2) Then fl1 = 0: Return
       Next j2
    Next j1
    Return

'   Check Pairs

950 fl1 = 1: n25 = 0
    For j1 = 1 To 9
       a2 = Pr7 - a(j1)          'Complement
       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