Vorige Pagina Volgende Pagina About the Author

' Constructs 9 x 9 Composed Magic Squares for Prime Numbers (Part 2)
' Semi Magic Sub Squares, Optimized for Sophie Germain Primes

' Tested with Office 2007 under Windows 7

Sub Priem9b2()

Dim a1(1260), b1(174299), b(174299), c(36), a(9), a9(81)

y = MsgBox("Locked", vbCritical, "Routine Priem9b2")
End
    
    n1 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
    ShtNm1 = "Pairs2"
    
    Sheets("Klad1").Select
    t1 = Timer

'   Generate Squares

For j100 = 2 To 7

    s3 = Sheets("Lines9").Cells(j100, 82).Value / 3   'MC3

    Rcrd1a = Sheets("Lines9").Cells(j100, 83).Value   'Record
    
    nVar = Sheets(ShtNm1).Cells(Rcrd1a, 9).Value
   
    If nVar < 81 Then GoTo 1000
    m1 = 1: m2 = nVar

    For i1 = 1 To m2
        a1(i1) = Sheets(ShtNm1).Cells(Rcrd1a, i1 + 9).Value
    Next i1

'   Extended Range

    Erase b1
    For i1 = 1 To 1055
        x = Sheets("SG1").Cells(i1, 1).Value
        b1(x) = x
    Next i1

'   Read Magic Center and Border Squares
'   Asign to a9() and and Remove from b1()

    For i1 = 1 To 81
        a9(i1) = Sheets("Lines9").Cells(j100, i1).Value
        b1(a9(i1)) = 0
    Next i1

'   Generate Semi Magic Sub Squares

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

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

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

For j5 = m1 To m2                                                     'a(5)
If b1(a1(j5)) = 0 Then GoTo 95
If b(a1(j5)) = 0 Then b(a1(j5)) = a1(j5): c(5) = a1(j5) Else GoTo 95
a(5) = a1(j5)
    
    a(4) = s3 - a(5) - a(6)
    If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 85:
    If b1(a(4)) = 0 Then GoTo 85
    
    a(3) = -a(6) + a(7) + a(8)
    If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 85:
    If b1(a(3)) = 0 Then GoTo 85
    
    a(2) = s3 - a(5) - a(8)
    If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 85:
    If b1(a(2)) = 0 Then GoTo 85
    
    a(1) = a(5) + a(6) - a(7)
    If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 85:
    If b1(a(1)) = 0 Then GoTo 85

'              Exclude solutions with identical numbers

               GoSub 800: If fl1 = 0 Then GoTo 85
                          
               n10 = n10 + 1
               Select Case n10
                      
               Case 1
                      GoSub 750: GoSub 900              'Assign Sub Square 1, Remove used primes
                      Erase b, c: GoTo 155
               Case 2
                      GoSub 750: GoSub 900              'Assign Sub Square 2, Remove used primes
                      Erase b, c: GoTo 155
               Case 3
                      GoSub 750: GoSub 900              'Assign Sub Square 3, Remove used primes
                      Erase b, c: GoTo 155
               Case 4
                      GoSub 750                         'Assign Sub Square 4
                      GoSub 850                         'Double Check Identical Integers
                      If fl1 = 1 Then
                             n9 = n9 + 1: GoSub 660     'Print Composed Squares
                      End If
                      Erase b, c: GoTo 1000             'Only four Sub Squares required, next j100
               End Select

85  b(c(5)) = 0: c(5) = 0
95  Next j5
   
    b(c(6)) = 0: c(6) = 0
105 Next j6
   
130 b(c(8)) = 0: c(8) = 0
135 Next j8
    
    b(c(9)) = 0: c(9) = 0
155 Next j9

1000  n10 = 0
      Next j100

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

End

'   Print results (selected numbers)

640 Cells(n9, 9).Select
    For i1 = 1 To 81
        Cells(n9, i1).Value = a9(i1)
    Next i1
    Cells(n9, 82).Value = 3 * s3
    Return

'   Print results (squares 9 x 9)

660 n1 = n1 + 1
    If n1 = 3 Then
        n1 = 1: k1 = k1 + 10: k2 = 1
    Else
        If n9 > 1 Then k2 = k2 + 10
    End If
    
    Cells(k1, k2 + 1).Select
    Cells(k1, k2 + 1).Font.Color = -4165632
    
    Cells(k1, k2 + 1).Value = CStr(3 * s3)
    
    i3 = 0
    For i1 = 1 To 9
        For i2 = 1 To 9
            i3 = i3 + 1
            Cells(k1 + i1, k2 + i2).Value = a9(i3)
        Next i2
    Next i1
    
    Return
   
'    Exclude solutions with identical numbers a9()

850  fl1 = 1
     For j1 = 1 To 81
        a20 = a9(j1): If a20 = 0 Then GoTo 860
        For j2 = (1 + j1) To 81
            If a20 = a9(j2) Then fl1 = 0: Return
        Next j2
860  Next j1
     Return
     
'    Assign Sub Squares

750  Select Case n10

        Case 1: 'Left
                
            a9(28) = a(1):  a9(29) = a(2):  a9(30) = a(3):
            a9(37) = a(4):  a9(38) = a(5):  a9(39) = a(6):
            a9(46) = a(7):  a9(47) = a(8):  a9(48) = a(9):
                
        Case 2: 'Top
        
            a9(4) = a(1):   a9(5) = a(2):   a9(6) = a(3):
            a9(13) = a(4):  a9(14) = a(5):  a9(15) = a(6):
            a9(22) = a(7):  a9(23) = a(8):  a9(24) = a(9):
                
        Case 3: 'Right
        
            a9(34) = a(1):  a9(35) = a(2):  a9(36) = a(3):
            a9(43) = a(4):  a9(44) = a(5):  a9(45) = a(6):
            a9(52) = a(7):  a9(53) = a(8):  a9(54) = a(9):
 
        Case 4: 'Bottom
        
            a9(58) = a(1):  a9(59) = a(2):  a9(60) = a(3):
            a9(67) = a(4):  a9(68) = a(5):  a9(69) = a(6):
            a9(76) = a(7):  a9(77) = a(8):  a9(78) = a(9):
     
     End Select
     
     Return

'    Exclude solutions with identical numbers a()

800  fl1 = 1
     For j1 = 1 To 9
        a20 = a(j1)
        For j2 = (1 + j1) To 9
            If a20 = a(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
     Next i1
     Return

End Sub

Vorige Pagina Volgende Pagina About the Author