Vorige Pagina About the Author

' Constructs Simple Magic Squares of order 12, composed of
' 8 Semi Magic Diagonal Squares (7 Magic Lines) and 8 Semi Magic Border Squares (6 Magic Lines)

' Tested with Office 2007 under Windows 7

Sub Priem3f1()

Dim a1(1200), b1(21803), a(9), b(21803), c(9), a12(144), a3(144)
Dim a2(9), b2(21803), c2(9)

y = MsgBox("Locked", vbCritical, "Routine Priem3f1")
End
    
    n1 = 0: n3 = 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 = 1217 To 8388

    GoSub 3010                                  'Read Prime Numbers From Sheet ShtNm1
    If nVar < 144 Then GoTo 1000
    If nSemi3 < 8 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
                          
                          n10 = n10 + 1                          
                          If n10 = 8 Then
                             GoSub 750                       ' Load Square Nr 8
                             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 ... 7
                             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: n3 = 0
      Next j100

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

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

                      n10 = n10 + 1
                      If n10 < 16 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 = 16 Then Erase b2, c2: Return         'Sixteen 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 + 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 = "MC = " + CStr(s2)
    
     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

'    Transform and Assign Sub Squares

750  Select Case n10

    'Diagonal Squares, Clockwise, Outer/Inner

        Case 1:
        a12(1) = a(3):  a12(2) = a(2):  a12(3) = a(1):
        a12(13) = a(6): a12(14) = a(5): a12(15) = a(4):
        a12(25) = a(9): a12(26) = a(8): a12(27) = a(7):
        
        Case 2:
        a12(10) = a(1): a12(11) = a(2): a12(12) = a(3):
        a12(22) = a(4): a12(23) = a(5): a12(24) = a(6):
        a12(34) = a(7): a12(35) = a(8): a12(36) = a(9):
        
        Case 3:
        a12(118) = a(3): a12(119) = a(2): a12(120) = a(1):
        a12(130) = a(6): a12(131) = a(5): a12(132) = a(4):
        a12(142) = a(9): a12(143) = a(8): a12(144) = a(7):
        
        Case 4:
        a12(109) = a(1): a12(110) = a(2): a12(111) = a(3):
        a12(121) = a(4): a12(122) = a(5): a12(123) = a(6):
        a12(133) = a(7): a12(134) = a(8): a12(135) = a(9):
        
        Case 5:
        a12(40) = a(3): a12(41) = a(2): a12(42) = a(1):
        a12(52) = a(6): a12(53) = a(5): a12(54) = a(4):
        a12(64) = a(9): a12(65) = a(8): a12(66) = a(7):
        
        Case 6:
        a12(43) = a(1): a12(44) = a(2): a12(45) = a(3):
        a12(55) = a(4): a12(56) = a(5): a12(57) = a(6):
        a12(67) = a(7): a12(68) = a(8): a12(69) = a(9):
        
        Case 7:
        a12(79) = a(3):  a12(80) = a(2):  a12(81) = a(1):
        a12(91) = a(6):  a12(92) = a(5):  a12(93) = a(4):
        a12(103) = a(9): a12(104) = a(8): a12(105) = a(7):
        
        Case 8:
        a12(76) = a(1):  a12(77) = a(2):  a12(78) = a(3):
        a12(88) = a(4):  a12(89) = a(5):  a12(90) = a(6):
        a12(100) = a(7): a12(101) = a(8): a12(102) = a(9):

    'Border Squares, Clockwise, Outer/Inner

        Case 9:
        a12(4) = a2(1):  a12(5) = a2(2):  a12(6) = a2(3):
        a12(16) = a2(4): a12(17) = a2(5): a12(18) = a2(6):
        a12(28) = a2(7): a12(29) = a2(8): a12(30) = a2(9):
        
        Case 10:
        a12(7) = a2(1):  a12(8) = a2(2):  a12(9) = a2(3):
        a12(19) = a2(4): a12(20) = a2(5): a12(21) = a2(6):
        a12(31) = a2(7): a12(32) = a2(8): a12(33) = a2(9):

        Case 11:
        a12(46) = a2(1): a12(47) = a2(2): a12(48) = a2(3):
        a12(58) = a2(4): a12(59) = a2(5): a12(60) = a2(6):
        a12(70) = a2(7): a12(71) = a2(8): a12(72) = a2(9):
        
        Case 12:
        a12(82) = a2(1):  a12(83) = a2(2):  a12(84) = a2(3):
        a12(94) = a2(4):  a12(95) = a2(5):  a12(96) = a2(6):
        a12(106) = a2(7): a12(107) = a2(8): a12(108) = a2(9):

        Case 13:
        a12(115) = a2(1): a12(116) = a2(2): a12(117) = a2(3):
        a12(127) = a2(4): a12(128) = a2(5): a12(129) = a2(6):
        a12(139) = a2(7): a12(140) = a2(8): a12(141) = a2(9):
        
        Case 14:
        a12(112) = a2(1): a12(113) = a2(2): a12(114) = a2(3):
        a12(124) = a2(4): a12(125) = a2(5): a12(126) = a2(6):
        a12(136) = a2(7): a12(137) = a2(8): a12(138) = a2(9):

        Case 15:
        a12(73) = a2(1):  a12(74) = a2(2): a12(75) = a2(3):
        a12(85) = a2(4):  a12(86) = a2(5): a12(87) = a2(6):
        a12(97) = a2(7):  a12(98) = a2(8): a12(99) = a2(9):
        
        Case 16:
        a12(37) = a2(1): a12(38) = a2(2): a12(39) = a2(3):
        a12(49) = a2(4): a12(50) = a2(5): a12(51) = a2(6):
        a12(61) = a2(7): a12(62) = a2(8): a12(63) = a2(9):

     End Select

     Return

'    Exclude solutions with identical numbers a12()

850  fl1 = 1
     For j1 = 1 To 144
        a20 = a12(j1)
        For j2 = (1 + j1) To 144
            If a20 = a12(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
     
'    Remove used primes a2() from available primes b1()

910  For i1 = 1 To 9
         b1(a2(i1)) = 0
     Next i1
     Return
    
'    Read Prime Numbers From Sheet ShtNm1

3010 Pr3 = Sheets(ShtNm1).Cells(j100, 1).Value    'Pair Sum
     s1 = 3 * Pr3 / 2                             'MC3
     s2 = 6 * Pr3                                 'MC12
     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