Vorige Pagina Volgende Pagina About the Author

' Constructs 12 x 12 Composed Magic Squares for Prime Numbers (Part 2)
' Eight order 3 Semi Magic Border Squares

' Tested with Office 2007 under Windows 7

Sub Priem12b2()

Dim a1(1260), b1(187141), b(187141), c(36), a(9), a12(144)

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

'   Generate Squares

For j100 = 2 To 43
s3 = Sheets("Lines12").Cells(j100, 145).Value / 4   'MC3
m1 = Sheets("Lines12").Cells(j100, 146).Value
m2 = Sheets("Lines12").Cells(j100, 147).Value

    For i1 = 1 To m2
        a1(i1) = Sheets(ShtNm1).Cells(i1, 3).Value
    Next i1
    pmax = a1(m2)

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

'   Read Magic Center and Border Square
'   Assign to a12() and and Remove from b1()
 
    Erase a12
    For i1 = 1 To 144
        a12(i1) = Sheets("Lines12").Cells(j100, i1).Value
        b1(a12(i1)) = 0
    Next i1
    
'   Restore available pairs in a1()

    Erase 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

'   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: GoSub 900                   'Assign Sub Square 4, Remove used primes
                             Erase b, c: GoTo 155
                      Case 5
                             GoSub 750: GoSub 900                   'Assign Sub Square 5, Remove used primes
                             Erase b, c: GoTo 155
                      Case 6
                             GoSub 750: GoSub 900                   'Assign Sub Square 6, Remove used primes
                             Erase b, c: GoTo 155
                      Case 7
                             GoSub 750: GoSub 900                   'Assign Sub Square 7, Remove used primes
                             Erase b, c: GoTo 155
                      Case 8
                             GoSub 750                              'Assign Sub Square 8
                             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

'   Not found

GoSub 850                              'Double Check Identical Integers
If fl1 = 1 Then
       n9 = n9 + 1: GoSub 660          'Print Composed Squares
End If
Erase b, c

1000  n10 = 0
      Next j100

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

End

'   Print results (selected numbers)

640 Cells(n9, 9).Select
    For i1 = 1 To 144
        Cells(n9, i1).Value = a12(i1)
    Next i1
    Cells(n9, 145).Value = 4 * s3
    Return

'   Print results (squares 12 x 12)

660 n1 = n1 + 1
    If n1 = 2 Then
        n1 = 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 = CStr(4 * s3)
    
    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
   
'    Exclude solutions with identical numbers a12()

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

750  Select Case n10

        Case 1: 'Top / Left
                
            a12(4) = a(1):  a12(5) = a(2):  a12(6) = a(3):
            a12(16) = a(4): a12(17) = a(5): a12(18) = a(6):
            a12(28) = a(7): a12(29) = a(8): a12(30) = a(9):
                
        Case 2: 'Top / Right

            a12(7) = a(1):  a12(8) = a(2):  a12(9) = a(3):
            a12(19) = a(4): a12(20) = a(5): a12(21) = a(6):
            a12(31) = a(7): a12(32) = a(8): a12(33) = a(9):
                
        Case 3: 'Right / Top
 
            a12(46) = a(1): a12(47) = a(2): a12(48) = a(3):
            a12(58) = a(4): a12(59) = a(5): a12(60) = a(6):
            a12(70) = a(7): a12(71) = a(8): a12(72) = a(9):
 
        Case 4: 'Left / Bottom
       
            a12(82) = a(1):  a12(83) = a(2):  a12(84) = a(3):
            a12(94) = a(4):  a12(95) = a(5):  a12(96) = a(6):
            a12(106) = a(7): a12(107) = a(8): a12(108) = a(9):
       
        Case 5: 'Bottom / Right
                
            a12(115) = a(1): a12(116) = a(2): a12(117) = a(3):
            a12(127) = a(4): a12(128) = a(5): a12(129) = a(6):
            a12(139) = a(7): a12(140) = a(8): a12(141) = a(9):
                
        Case 6: 'Bottom / Left
                
            a12(112) = a(1): a12(113) = a(2): a12(114) = a(3):
            a12(124) = a(4): a12(125) = a(5): a12(126) = a(6):
            a12(136) = a(7): a12(137) = a(8): a12(138) = a(9):
                
        Case 7: 'Left / Bottom

            a12(73) = a(1): a12(74) = a(2): a12(75) = a(3):
            a12(85) = a(4): a12(86) = a(5): a12(87) = a(6):
            a12(97) = a(7): a12(98) = a(8): a12(99) = a(9):
 
        Case 8: 'Left / Top
     
            a12(37) = a(1): a12(38) = a(2): a12(39) = a(3):
            a12(49) = a(4): a12(50) = a(5): a12(51) = a(6):
            a12(61) = a(7): a12(62) = a(8): a12(63) = 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