Vorige Pagina About the Author

' Constructs 18 x 18 Primme Number Associated Magic Squares composed of
' 12 Semi Magic Anti Symmetric Diagonal Squares and 24 Semi Magic Anti Symmetric Border Squares

' Tested with Office 2007 under Windows 7

Sub Priem3f4()

Dim a1(1200), b1(21803), a(9), b(21803), c(9), a18(324)
Dim a2(9), b2(21803), c2(9)

y = MsgBox("Locked", vbCritical, "Routine Priem3f4")
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 = 6425 To 8388

    GoSub 3010                                  'Read Prime Numbers From Sheet ShtNm1
    If nVar < 324 Then GoTo 1000
    If nSemi3 < 18 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 = 6 Then
                             GoSub 750                          ' Load Square Nr 6
                             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, 3, 4 or 5
                             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 Priem3f4")

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 < 18 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 = 18 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 + 19: k2 = 1
     Else
         If n9 > 1 Then k2 = k2 + 19
     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 18
         For i2 = 1 To 18
             i3 = i3 + 1
             Cells(k1 + i1, k2 + i2).Value = a18(i3)
         Next i2
     Next i1
    
     Return

'    Transform and Assign Sub Squares

750  Select Case n10

    'Diagonal Squares, Outer/Inner

        Case 1:
        a18(1) = a(3):  a18(2) = a(2):  a18(3) = a(1):
        a18(19) = a(6): a18(20) = a(5): a18(21) = a(4):
        a18(37) = a(9): a18(38) = a(8): a18(39) = a(7):
        
        Case 2:
        a18(58) = a(3): a18(59) = a(2): a18(60) = a(1):
        a18(76) = a(6): a18(77) = a(5): a18(78) = a(4):
        a18(94) = a(9): a18(95) = a(8): a18(96) = a(7):
       
        Case 3:
        a18(115) = a(3): a18(116) = a(2): a18(117) = a(1):
        a18(133) = a(6): a18(134) = a(5): a18(135) = a(4):
        a18(151) = a(9): a18(152) = a(8): a18(153) = a(7):
        
        Case 4:
        a18(118) = a(1): a18(119) = a(2): a18(120) = a(3):
        a18(136) = a(4): a18(137) = a(5): a18(138) = a(6):
        a18(154) = a(7): a18(155) = a(8): a18(156) = a(9):
        
        Case 5:
        a18(67) = a(1):  a18(68) = a(2):  a18(69) = a(3):
        a18(85) = a(4):  a18(86) = a(5):  a18(87) = a(6):
        a18(103) = a(7): a18(104) = a(8): a18(105) = a(9):
        
        Case 6:
        a18(16) = a(1): a18(17) = a(2): a18(18) = a(3):
        a18(34) = a(4): a18(35) = a(5): a18(36) = a(6):
        a18(52) = a(7): a18(53) = a(8): a18(54) = a(9):
 
    'Border Squares, Outer/Inner

        Case 7:
        a18(4) = a2(1):  a18(5) = a2(2):  a18(6) = a2(3):
        a18(22) = a2(4): a18(23) = a2(5): a18(24) = a2(6):
        a18(40) = a2(7): a18(41) = a2(8): a18(42) = a2(9):
        
        Case 8:
        a18(7) = a2(1):  a18(8) = a2(2):  a18(9) = a2(3):
        a18(25) = a2(4): a18(26) = a2(5): a18(27) = a2(6):
        a18(43) = a2(7): a18(44) = a2(8): a18(45) = a2(9):

        Case 9:
        a18(10) = a2(1): a18(11) = a2(2): a18(12) = a2(3):
        a18(28) = a2(4): a18(29) = a2(5): a18(30) = a2(6):
        a18(46) = a2(7): a18(47) = a2(8): a18(48) = a2(9):
        
        Case 10:
        a18(13) = a2(1): a18(14) = a2(2): a18(15) = a2(3):
        a18(31) = a2(4): a18(32) = a2(5): a18(33) = a2(6):
        a18(49) = a2(7): a18(50) = a2(8): a18(51) = a2(9):
        
        Case 11:
        a18(55) = a2(1): a18(56) = a2(2): a18(57) = a2(3):
        a18(73) = a2(4): a18(74) = a2(5): a18(75) = a2(6):
        a18(91) = a2(7): a18(92) = a2(8): a18(93) = a2(9):
        
        Case 12:
        a18(61) = a2(1): a18(62) = a2(2): a18(63) = a2(3):
        a18(79) = a2(4): a18(80) = a2(5): a18(81) = a2(6):
        a18(97) = a2(7): a18(98) = a2(8): a18(99) = a2(9):

        Case 13:
        a18(64) = a2(1):  a18(65) = a2(2):  a18(66) = a2(3):
        a18(82) = a2(4):  a18(83) = a2(5):  a18(84) = a2(6):
        a18(100) = a2(7): a18(101) = a2(8): a18(102) = a2(9):
        
        Case 14:
        a18(70) = a2(1):  a18(71) = a2(2):  a18(72) = a2(3):
        a18(88) = a2(4):  a18(89) = a2(5):  a18(90) = a2(6):
        a18(106) = a2(7): a18(107) = a2(8): a18(108) = a2(9):
        
        Case 15:
        a18(109) = a2(1): a18(110) = a2(2): a18(111) = a2(3):
        a18(127) = a2(4): a18(128) = a2(5): a18(129) = a2(6):
        a18(145) = a2(7): a18(146) = a2(8): a18(147) = a2(9):
        
        Case 16:
        a18(112) = a2(1): a18(113) = a2(2): a18(114) = a2(3):
        a18(130) = a2(4): a18(131) = a2(5): a18(132) = a2(6):
        a18(148) = a2(7): a18(149) = a2(8): a18(150) = a2(9):

        Case 17:
        a18(121) = a2(1): a18(122) = a2(2): a18(123) = a2(3):
        a18(139) = a2(4): a18(140) = a2(5): a18(141) = a2(6):
        a18(157) = a2(7): a18(158) = a2(8): a18(159) = a2(9):
        
        Case 18:
        a18(124) = a2(1): a18(125) = a2(2): a18(126) = a2(3):
        a18(142) = a2(4): a18(143) = a2(5): a18(144) = a2(6):
        a18(160) = a2(7): a18(161) = a2(8): a18(162) = a2(9):
        
'       Assign Associated Elements

        For i1 = 1 To 162
                a18(325 - i1) = Pr3 - a18(i1)
        Next i1

     End Select

     Return

'    Exclude solutions with identical numbers a18()

850  fl1 = 1
     For j1 = 1 To 324
        a20 = a18(j1)
        For j2 = (1 + j1) To 324
            If a20 = a18(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 = 9 * Pr3                                 'MC18
     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