Vorige Pagina About the Author

' Constructs 11 x 11 Primme Number Magic Squares based on:
' One Magic Center Square (3 x 3), four Magic Associated Corner Squares (4 x 4),
' Four Associated Magic Border Rectagles (3 x 4)


' Tested with Office 2007 under Windows 7

Sub Priem11e()

    Dim a1(1944), a11(121), a(16), b1(43300), b(43300), c(16), a2(16), b2(43300), c2(16), a3(121)

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

    n2 = 0: n3 = 0: k1 = 1: k2 = 1: n9 = 0: n10 = 0
    Sht1 = "Pairs3"

'   Generate Squares

    Sheets("Klad1").Select
    
    t1 = Timer

For j100 = 523 To 704 ''2410

    GoSub 2010                    'Read Prime Numbers From Sheet Sht1
    
    If m2 < 161 Then GoTo 1000

'   Determine Center Square

For j9 = m1 To m2                                                     'a(9)
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 b(a1(j8)) = 0 Then b(a1(j8)) = a1(j8): c(8) = a1(j8) Else GoTo 120
a(8) = a1(j8)

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


'                         Exclude solutions with identical numbers a()

                          GoSub 800: If fl1 = 0 Then GoTo 110
                          
                          GoSub 700                           'Assign Center Square
                          GoSub 900                           'Remove used primes a() from available primes b1()
                             
                          GoSub 2000                          'Determine 4 Corner Squares
                          If n10 < 4 Then n10 = 0: n3 = 0: Erase b, c: GoTo 1000
                          
                          GoSub 3000                          'Determine 4 Border Rectagles
                          n10 = 0: n3 = 0: Erase b, c: GoTo 1000


110 b(c(8)) = 0: c(8) = 0
120 Next j8
    
    b(c(9)) = 0: c(9) = 0
160 Next j9
     
     n10 = 0
     
1000 Next j100

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

End

'    Determine 4 Corner Squares

2000 For jj16 = m1 To m2                                          'a2(16)
     If b1(a1(jj16)) = 0 Then GoTo 165
     If b2(a1(jj16)) = 0 Then b2(a1(jj16)) = a1(jj16): c2(16) = a1(jj16) Else GoTo 165
     a2(16) = a1(jj16)
        
     a2(1) = 0.5 * s4 - a2(16): If b2(a2(1)) = 0 Then b2(a2(1)) = a2(1): c2(1) = a2(1) Else GoTo 10
    
     For jj15 = m1 To m2                                          'a2(15)
     If b1(a1(jj15)) = 0 Then GoTo 150
     If b2(a1(jj15)) = 0 Then b2(a1(jj15)) = a1(jj15): c2(15) = a1(jj15) Else GoTo 150
     a2(15) = a1(jj15)
        
     a2(2) = 0.5 * s4 - a2(15): If b2(a2(2)) = 0 Then b2(a2(2)) = a2(2): c2(2) = a2(2) Else GoTo 20
        
     For jj14 = m1 To m2                                          'a2(14)
     If b1(a1(jj14)) = 0 Then GoTo 140
     If b2(a1(jj14)) = 0 Then b2(a1(jj14)) = a1(jj14): c2(14) = a1(jj14) Else GoTo 140
     a2(14) = a1(jj14)
        
     a2(13) = s4 - a2(14) - a2(15) - a2(16)
     If a2(13) < a1(m1) Or a2(13) > a1(m2) Then GoTo 130
     If b1(a2(13)) = 0 Then GoTo 130
     If b2(a2(13)) = 0 Then b2(a2(13)) = a2(13): c2(13) = a2(13) Else GoTo 130
        
     a2(4) = 0.5 * s4 - a2(13): If b2(a2(4)) = 0 Then b2(a2(4)) = a2(4): c2(4) = a2(4) Else GoTo 40
     a2(3) = 0.5 * s4 - a2(14): If b2(a2(3)) = 0 Then b2(a2(3)) = a2(3): c2(3) = a2(3) Else GoTo 30
        
     For jj12 = m1 To m2                                          'a2(12)
     If b1(a1(jj12)) = 0 Then GoTo 125
     If b2(a1(jj12)) = 0 Then b2(a1(jj12)) = a1(jj12): c2(12) = a1(jj12) Else GoTo 125
     a2(12) = a1(jj12)
        
     a2(11) = s4 - a2(12) - a2(15) - a2(16)
     If a2(11) < a1(m1) Or a2(11) > a1(m2) Then GoTo 115
     If b1(a2(11)) = 0 Then GoTo 115
     If b2(a2(11)) = 0 Then b2(a2(11)) = a2(11): c2(11) = a2(11) Else GoTo 115
    
     a2(10) = s4 - a2(12) - a2(14) - a2(16)
     If a2(10) < a1(m1) Or a2(10) > a1(m2) Then GoTo 100
     If b1(a2(10)) = 0 Then GoTo 100
     If b2(a2(10)) = 0 Then b2(a2(10)) = a2(10): c2(10) = a2(10) Else GoTo 100
        
     a2(9) = s4 - a2(10) - a2(11) - a2(12)
     If a2(9) < a1(m1) Or a2(9) > a1(m2) Then GoTo 90
     If b1(a2(9)) = 0 Then GoTo 90
     If b2(a2(9)) = 0 Then b2(a2(9)) = a2(9): c2(9) = a2(9) Else GoTo 90
        
     a2(8) = 0.5 * s4 - a2(9): If b2(a2(8)) = 0 Then b2(a2(8)) = a2(8): c2(8) = a2(8) Else GoTo 80
     a2(7) = 0.5 * s4 - a2(10): If b2(a2(7)) = 0 Then b2(a2(7)) = a2(7): c2(7) = a2(7) Else GoTo 75
     a2(6) = 0.5 * s4 - a2(11): If b2(a2(6)) = 0 Then b2(a2(6)) = a2(6): c2(6) = a2(6) Else GoTo 60
     a2(5) = 0.5 * s4 - a2(12): If b2(a2(5)) = 0 Then b2(a2(5)) = a2(5): c2(5) = a2(5) Else GoTo 50
                      
                      
                      n10 = n10 + 1
                      
                      If n10 < 4 Then
                             GoSub 750                              'Transform and Assign Corner Squares
                             n32 = 16: GoSub 910                    'Remove used primes a2() from available primes b1()
                             Erase b2, c2: GoTo 165
                      Else
                             GoSub 750                              'Transform and Assign Corner Squares
                             n32 = 16: GoSub 910                    'Remove used primes a2() from available primes b1()
                      End If
                      If n10 = 4 Then Erase b2, c2: Return          'Only four squares required


    b2(c2(5)) = 0: c2(5) = 0
50  b2(c2(6)) = 0: c2(6) = 0
60  b2(c2(7)) = 0: c2(7) = 0
75  b2(c2(8)) = 0: c2(8) = 0
80  b2(c2(9)) = 0: c2(9) = 0
90  b2(c2(10)) = 0: c2(10) = 0
100 b2(c2(11)) = 0: c2(11) = 0
115 b2(c2(12)) = 0: c2(12) = 0
125 Next jj12

    b2(c2(3)) = 0: c2(3) = 0
30  b2(c2(4)) = 0: c2(4) = 0
40  b2(c2(13)) = 0: c2(13) = 0
130 b2(c2(14)) = 0: c2(14) = 0
140 Next jj14

    b2(c2(2)) = 0: c2(2) = 0
20  b2(c2(15)) = 0: c2(15) = 0
150 Next jj15

    b2(c2(1)) = 0: c2(1) = 0
10  b2(c2(16)) = 0: c2(16) = 0
165 Next jj16

    Return

'   Determine Magic Rectangles 3 x 4

3000 Erase b2, c2

For jjj12 = m1 To m2                                                     'a2(12)
If b1(a1(jjj12)) = 0 Then GoTo 3120
If b2(a1(jjj12)) = 0 Then b2(a1(jjj12)) = a1(jjj12): c2(12) = a1(jjj12) Else GoTo 3120
a2(12) = a1(jjj12)

    a2(1) = Pr3 - a2(12): If b2(a2(1)) = 0 Then b2(a2(1)) = a2(1): c2(1) = a2(1) Else GoTo 3010

For jjj11 = m1 To m2                                                     'a2(11)
If b1(a1(jjj11)) = 0 Then GoTo 3110
If b2(a1(jjj11)) = 0 Then b2(a1(jjj11)) = a1(jjj11): c2(11) = a1(jjj11) Else GoTo 3110
a2(11) = a1(jjj11)
    
    a2(2) = Pr3 - a2(11): If b2(a2(2)) = 0 Then b2(a2(2)) = a2(2): c2(2) = a2(2) Else GoTo 3020
    
For jjj10 = m1 To m2                                                     'a2(10)
If b1(a1(jjj10)) = 0 Then GoTo 3100
If b2(a1(jjj10)) = 0 Then b2(a1(jjj10)) = a1(jjj10): c2(10) = a1(jjj10) Else GoTo 3100
a2(10) = a1(jjj10)

    a2(9) = 2 * Pr3 - a2(10) - a2(11) - a2(12)
    If a2(9) < a1(m1) Or a2(9) > a1(m2) Then GoTo 3090:
    If b1(a2(9)) = 0 Then GoTo 3090
    If b2(a2(9)) = 0 Then b2(a2(9)) = a2(9): c2(9) = a2(9) Else GoTo 3090

    a2(8) = 5 * Pr3 / 2 - a2(10) - a2(11) - 2 * a2(12)
    If a2(8) < a1(m1) Or a2(8) > a1(m2) Then GoTo 3080:
    If b1(a2(8)) = 0 Then GoTo 3080
    If b2(a2(8)) = 0 Then b2(a2(8)) = a2(8): c2(8) = a2(8) Else GoTo 3080

    a2(7) = 3 * Pr3 - a2(8) - 2 * a2(11) - 2 * a2(12)
    If a2(7) < a1(m1) Or a2(7) > a1(m2) Then GoTo 3070:
    If b1(a2(7)) = 0 Then GoTo 3070
    If b2(a2(7)) = 0 Then b2(a2(7)) = a2(7): c2(7) = a2(7) Else GoTo 3070

    a2(6) = Pr3 - a2(7): If b2(a2(6)) = 0 Then b2(a2(6)) = a2(6): c2(6) = a2(6) Else GoTo 3060
    a2(5) = Pr3 - a2(8): If b2(a2(5)) = 0 Then b2(a2(5)) = a2(5): c2(5) = a2(5) Else GoTo 3050
    a2(4) = Pr3 - a2(9): If b2(a2(4)) = 0 Then b2(a2(4)) = a2(4): c2(4) = a2(4) Else GoTo 3040
    a2(3) = Pr3 - a2(10): If b2(a2(3)) = 0 Then b2(a2(3)) = a2(3): c2(3) = a2(3) Else GoTo 3030

                      n10 = n10 + 1
                     
                      If n10 < 8 Then
                             GoSub 750                              'Transform and Assign Border Squares
                             n32 = 12: GoSub 910                    'Remove used primes a2() from available primes b1()
                             Erase b2, c2: GoTo 3120
                      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          'Only four rectangles required

     b2(c2(3)) = 0: c2(3) = 0
3030 b2(c2(4)) = 0: c2(4) = 0
3040 b2(c2(5)) = 0: c2(5) = 0
3050 b2(c2(6)) = 0: c2(6) = 0
3060 b2(c2(7)) = 0: c2(7) = 0
3070 b2(c2(8)) = 0: c2(8) = 0
3080 b2(c2(9)) = 0: c2(9) = 0
3090 b2(c2(10)) = 0: c2(10) = 0
3100 Next jjj10
   
     b2(c2(2)) = 0: c2(2) = 0
3020 b2(c2(11)) = 0: c2(11) = 0
3110 Next jjj11
    
     b2(c2(1)) = 0: c2(1) = 0
3010 b2(c2(12)) = 0: c2(12) = 0
3120 Next jjj12
 
     Return

640  Cells(n9, 122).Select
     For i1 = 1 To 121
         Cells(n9, i1).Value = a11(i1)
     Next i1
     Cells(n9, 122).Value = s11
     Cells(n9, 123).Value = Rcrd1a
     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

'    Assign Center Square

700  a11(49) = a(1): a11(50) = a(2): a11(51) = a(3):
     a11(60) = a(4): a11(61) = a(5): a11(62) = a(6):
     a11(71) = a(7): a11(72) = a(8): a11(73) = a(9):
     Return

'    Transform and Assign Corner Squares and Border Rectangles

750  Select Case n10

        Case 1: 'Left  Top

            a11(1) = a2(1):   a11(2) = a2(2):   a11(3) = a2(3):   a11(4) = a2(4):
            a11(12) = a2(5):  a11(13) = a2(6):  a11(14) = a2(7):  a11(15) = a2(8):
            a11(23) = a2(9):  a11(24) = a2(10): a11(25) = a2(11): a11(26) = a2(12):
            a11(34) = a2(13): a11(35) = a2(14): a11(36) = a2(15): a11(37) = a2(16):
                
        Case 2: 'Right Top
        
            a11(8) = a2(1):   a11(9) = a2(2):   a11(10) = a2(3):  a11(11) = a2(4):
            a11(19) = a2(5):  a11(20) = a2(6):  a11(21) = a2(7):  a11(22) = a2(8):
            a11(30) = a2(9):  a11(31) = a2(10): a11(32) = a2(11): a11(33) = a2(12):
            a11(41) = a2(13): a11(42) = a2(14): a11(43) = a2(15): a11(44) = a2(16):
                
        Case 3: 'Right Bottom
        
            a11(85) = a2(1):   a11(86) = a2(2):   a11(87) = a2(3):   a11(88) = a2(4):
            a11(96) = a2(5):   a11(97) = a2(6):   a11(98) = a2(7):   a11(99) = a2(8):
            a11(107) = a2(9):  a11(108) = a2(10): a11(109) = a2(11): a11(110) = a2(12):
            a11(118) = a2(13): a11(119) = a2(14): a11(120) = a2(15): a11(121) = a2(16):
       
        Case 4: 'Left  Bottom
                
            a11(78) = a2(1):   a11(79) = a2(2):   a11(80) = a2(3):   a11(81) = a2(4):
            a11(89) = a2(5):   a11(90) = a2(6):   a11(91) = a2(7):   a11(92) = a2(8):
            a11(100) = a2(9):  a11(101) = a2(10): a11(102) = a2(11): a11(103) = a2(12):
            a11(111) = a2(13): a11(112) = a2(14): a11(113) = a2(15): a11(114) = a2(16):
        
        Case 5: 'Mid   Top
                
            a11(5) = a2(9):   a11(6) = a2(5):  a11(7) = a2(1):
            a11(16) = a2(10): a11(17) = a2(6): a11(18) = a2(2):
            a11(27) = a2(11): a11(28) = a2(7): a11(29) = a2(3):
            a11(38) = a2(12): a11(39) = a2(8): a11(40) = a2(4):
                
        Case 6: 'Mid   Right
        
            a11(52) = a2(1): a11(53) = a2(2):  a11(54) = a2(3):   a11(55) = a2(4):
            a11(63) = a2(5): a11(64) = a2(6):  a11(65) = a2(7):   a11(66) = a2(8):
            a11(74) = a2(9): a11(75) = a2(10): a11(76) = a2(11):  a11(77) = a2(12):
                
        Case 7: 'Mid   Bottom
        
            a11(82) = a2(9):   a11(83) = a2(5):  a11(84) = a2(1):
            a11(93) = a2(10):  a11(94) = a2(6):  a11(95) = a2(2):
            a11(104) = a2(11): a11(105) = a2(7): a11(106) = a2(3):
            a11(115) = a2(12): a11(116) = a2(8): a11(117) = a2(4):
       
        Case 8: 'Mid   Left
                
            a11(45) = a2(1): a11(46) = a2(2):  a11(47) = a2(3):  a11(48) = a2(4):
            a11(56) = a2(5): a11(57) = a2(6):  a11(58) = a2(7):  a11(59) = a2(8):
            a11(67) = a2(9): a11(68) = a2(10): a11(69) = a2(11): a11(70) = a2(12):
            
     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

'    Exclude solutions with identical numbers a2()

810  fl1 = 1
     For j1 = 1 To 16
        a20 = a2(j1)
        For j2 = (1 + j1) To 16
            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 primes a() from available primes b1()

900  For i1 = 1 To 9
         b1(a(i1)) = 0
     Next i1
     Return

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

910  For i1 = 1 To n32
         b1(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
     
'    Read Prime Numbers From Sheet Sht1

2010 Cntr3 = Sheets(Sht1).Cells(j100, 4).Value  'Center Element
     s3 = 3 * Cntr3                             'MC3
     s4 = 4 * Cntr3                             'MC4
     s11 = 11 * Cntr3                           'MC1
     nVar = Sheets(Sht1).Cells(j100, 5).Value
     Pr3 = Sheets(Sht1).Cells(j100, 1).Value    'Pair Sum
     nSemi3 = Sheets(Sht1).Cells(j100, 6).Value 'Expected Nmbr Semi Magic Squares
    
     m1 = 1: m2 = nVar
    
     For i1 = m1 To m2
         a1(i1) = Sheets(Sht1).Cells(j100, i1 + 6).Value
     Next i1
     If a1(1) = 1 Then m1 = 2: m2 = m2 - 1
    
     Erase b1
     For i1 = m1 To m2
         b1(a1(i1)) = a1(i1)
     Next i1
     Return

End Sub

Vorige Pagina About the Author