Vorige Pagina About the Author

' Generates Concentric Magic Squares of order 12 for Prime Numbers
' Optimized for Twin Squares

' Tested with Office 2007 under Windows 7

Sub Priem12a()

    Dim a1(2448), a(144), a3(40), a12(144), b1(98929), b(98929), c(144)
 
    y = MsgBox("Locked", vbCritical, "Routine Priem12a")
    End

    Sheets("Klad1").Select

    n5 = 0: n9 = 0: k1 = 1: k2 = 1: n3 = 0
    ShtNm1 = "Pairs6a"
    ShtNm2 = "Lines10"
    
    t1 = Timer

    For j100 = 10 To 28

'       Start Reading Data ShtNm2
    
        Rcrd1a = Sheets(ShtNm2).Cells(j100, 102).Value
        MC10 = Sheets(ShtNm2).Cells(j100, 101).Value

'       Read Prime Numbers From Sheet ShtNm1

        Pr4 = Sheets(ShtNm1).Cells(Rcrd1a, 1).Value      'PairSum
        
        s4 = 2 * Pr4                                     'MC4
        s10 = 5 * Pr4                                    'MC10
        s12 = 6 * Pr4                                    'MC12
        
        nVar = Sheets(ShtNm1).Cells(Rcrd1a, 5).Value
        If nVar < 144 Then GoTo 1000

        If MC10 <> s10 Then
                y = MsgBox("Conflict in Data", vbCritical, "Read " + ShtNm2 + " " + CStr(j100))
                End
        End If

        Erase b1
        For j1 = 1 To nVar
            x = Sheets(ShtNm1).Cells(Rcrd1a, 10 + j1).Value
            b1(x) = x
        Next j1
        pMax = Sheets(ShtNm1).Cells(Rcrd1a, 10 + nVar).Value

'       Read Concentric Square 10 x 10
       
        For i1 = 1 To 100
            a(i1) = Sheets(ShtNm2).Cells(j100, i1).Value
        Next i1
        n31 = 1: n32 = 100: GoSub 900                    'Remove used primes from available primes
        
        Erase a12

        a12(14) = a(1):   a12(15) = a(2):   a12(16) = a(3):   a12(17) = a(4):   a12(18) = a(5):   a12(19) = a(6):   a12(20) = a(7):
        a12(21) = a(8):   a12(22) = a(9):   a12(23) = a(10):
        a12(26) = a(11):  a12(27) = a(12):  a12(28) = a(13):  a12(29) = a(14):  a12(30) = a(15):  a12(31) = a(16):  a12(32) = a(17):
        a12(33) = a(18):  a12(34) = a(19):  a12(35) = a(20):
        a12(38) = a(21):  a12(39) = a(22):  a12(40) = a(23):  a12(41) = a(24):  a12(42) = a(25):  a12(43) = a(26):  a12(44) = a(27):
        a12(45) = a(28):  a12(46) = a(29):  a12(47) = a(30):
        a12(50) = a(31):  a12(51) = a(32):  a12(52) = a(33):  a12(53) = a(34):  a12(54) = a(35):  a12(55) = a(36):  a12(56) = a(37):
        a12(57) = a(38):  a12(58) = a(39):  a12(59) = a(40):
        a12(62) = a(41):  a12(63) = a(42):  a12(64) = a(43):  a12(65) = a(44):  a12(66) = a(45):  a12(67) = a(46):  a12(68) = a(47):
        a12(69) = a(48):  a12(70) = a(49):  a12(71) = a(50):
        a12(74) = a(51):  a12(75) = a(52):  a12(76) = a(53):  a12(77) = a(54):  a12(78) = a(55):  a12(79) = a(56):  a12(80) = a(57):
        a12(81) = a(58):  a12(82) = a(59):  a12(83) = a(60):
        a12(86) = a(61):  a12(87) = a(62):  a12(88) = a(63):  a12(89) = a(64):  a12(90) = a(65):  a12(91) = a(66):  a12(92) = a(67):
        a12(93) = a(68):  a12(94) = a(69):  a12(95) = a(70):
        a12(98) = a(71):  a12(99) = a(72):  a12(100) = a(73): a12(101) = a(74): a12(102) = a(75): a12(103) = a(76): a12(104) = a(77):
        a12(105) = a(78): a12(106) = a(79): a12(107) = a(80):
        a12(110) = a(81): a12(111) = a(82): a12(112) = a(83): a12(113) = a(84): a12(114) = a(85): a12(115) = a(86): a12(116) = a(87):
        a12(117) = a(88): a12(118) = a(89): a12(119) = a(90):
        a12(122) = a(91): a12(123) = a(92): a12(124) = a(93): a12(125) = a(94): a12(126) = a(95): a12(127) = a(96): a12(128) = a(97):
        a12(129) = a(98): a12(130) = a(99): a12(131) = a(100):

        Erase a

'       Restore available pairs in 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
        If a1(1) = 1 Then m1 = 2: m2 = m2 - 1

'       Generate Border

    For j1 = m1 To m2
    If b1(a1(j1)) = 0 Then GoTo 10
    If b(a1(j1)) = 0 Then b(a1(j1)) = a1(j1): c(1) = a1(j1) Else GoTo 10
    a(1) = a1(j1)
   
    a(5) = Pr4 - a(1): If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 50

    For j2 = m1 To m2
    If b1(a1(j2)) = 0 Then GoTo 20
    If b(a1(j2)) = 0 Then b(a1(j2)) = a1(j2): c(2) = a1(j2) Else GoTo 20
    a(2) = a1(j2)
    
    a(6) = Pr4 - a(2): If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 60

    For j3 = m1 To m2
    If b1(a1(j3)) = 0 Then GoTo 30
    If b(a1(j3)) = 0 Then b(a1(j3)) = a1(j3): c(3) = a1(j3) Else GoTo 30
    a(3) = a1(j3)

    a(7) = Pr4 - a(3): If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 70

    a(4) = s4 - a(3) - a(2) - a(1)
    If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 40:
    If b1(a(4)) = 0 Then GoTo 40
    If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 40

    a(8) = Pr4 - a(4): If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 80
        
    For j9 = m1 To m2
    If b1(a1(j9)) = 0 Then GoTo 90
    If b(a1(j9)) = 0 Then b(a1(j9)) = a1(j9): c(9) = a1(j9) Else GoTo 90
    a(9) = a1(j9)
   
    a(12) = Pr4 - a(9): If b(a(12)) = 0 Then b(a(12)) = a(12): c(12) = a(12) Else GoTo 120

    For j10 = m1 To m2
    If b1(a1(j10)) = 0 Then GoTo 100
    If b(a1(j10)) = 0 Then b(a1(j10)) = a1(j10): c(10) = a1(j10) Else GoTo 100
    a(10) = a1(j10)
    
    a(13) = Pr4 - a(10): If b(a(13)) = 0 Then b(a(13)) = a(13): c(13) = a(13) Else GoTo 130

    a(11) = s4 - a(10) - a(9) - a(1)
    If a(11) < a1(m1) Or a(11) > a1(m2) Then GoTo 110:
    If b1(a(11)) = 0 Then GoTo 110
    If b(a(11)) = 0 Then b(a(11)) = a(11): c(11) = a(11) Else GoTo 110

    a(14) = Pr4 - a(11): If b(a(14)) = 0 Then b(a(14)) = a(14): c(14) = a(14) Else GoTo 140
    
    n10 = n10 + 1
    
    If n10 = 1 Then
    
        a12(1) = a(1):   a12(2) = a(2):   a12(3) = a(3):   a12(4) = a(4):
        a12(144) = a(5): a12(134) = a(6): a12(135) = a(7): a12(136) = a(8):
    
        a12(13) = a(9):  a12(24) = a(12):
        a12(25) = a(10): a12(36) = a(13):
        a12(37) = a(11): a12(48) = a(14):
        
        n31 = 1: n32 = 14: GoSub 950                'Remove used primes and store in a3()

        Erase b, c: GoTo 10
        
    Else
    
        a12(9) = a(4):   a12(10) = a(3):   a12(11) = a(2): a12(12) = a(1):
        a12(141) = a(8): a12(142) = a(7): a12(143) = a(6): a12(133) = a(5):
        
        a12(97) = a(12): a12(108) = a(9):
        a12(109) = a(13): a12(120) = a(10):
        a12(121) = a(14): a12(132) = a(11):
        
        n31 = 1: n32 = 14: GoSub 950                'Remove used primes and store in a3()
                                                    
        Erase b, c: GoSub 2000                      'Complete Border with remaining segments (4 x 1)

        If fl1 = 1 Then
            GoSub 800:                              'Back Check Identical Numbers a12()
            If fl1 = 1 Then
               n9 = n9 + 1: GoSub 1650              'Print results (squares)
'              n9 = n9 + 1: GoSub 1640              'Print results (lines)
            End If
            Erase a3: n3 = 0: n10 = 0
            Erase b1, b, c: GoTo 1000               'Continue with next Magic Sum
        Else
            GoSub 955                               'Reassign removed primes and continue
            Erase b, c: GoTo 10
        End If
    
    End If

    b(c(14)) = 0: c(14) = 0
140 b(c(11)) = 0: c(11) = 0
110 b(c(13)) = 0: c(13) = 0
130 b(c(10)) = 0: c(10) = 0
100 Next j10

    b(c(12)) = 0: c(12) = 0
120 b(c(9)) = 0: c(9) = 0
90  Next j9

   b(c(8)) = 0: c(8) = 0
80 b(c(4)) = 0: c(4) = 0
40 b(c(7)) = 0: c(7) = 0
70 b(c(3)) = 0: c(3) = 0
30 Next j3

   b(c(6)) = 0: c(6) = 0
60 b(c(2)) = 0: c(2) = 0
20 Next j2

   b(c(5)) = 0: c(5) = 0
50 b(c(1)) = 0: c(1) = 0
10 Next j1

     n3 = 0: n10 = 0
     Erase b1, b, c
1000 Next j100
    
    t2 = Timer
    
    t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
    y = MsgBox(t10, 0, "Routine Priem12a")

End

'   Complete Border with remaining segments (4 x 1)

2000 fl1 = 1

    For j15 = m1 To m2
    If b1(a1(j15)) = 0 Then GoTo 150
    If b(a1(j15)) = 0 Then b(a1(j15)) = a1(j15): c(15) = a1(j15) Else GoTo 150
    a(15) = a1(j15)
    
    a(19) = Pr4 - a(15): If b(a(19)) = 0 Then b(a(19)) = a(19): c(19) = a(19) Else GoTo 190

    For j16 = m1 To m2
    If b1(a1(j16)) = 0 Then GoTo 160
    If b(a1(j16)) = 0 Then b(a1(j16)) = a1(j16): c(16) = a1(j16) Else GoTo 160
    a(16) = a1(j16)

    a(20) = Pr4 - a(16): If b(a(20)) = 0 Then b(a(20)) = a(20): c(20) = a(20) Else GoTo 200

    For j17 = m1 To m2
    If b1(a1(j17)) = 0 Then GoTo 170
    If b(a1(j17)) = 0 Then b(a1(j17)) = a1(j17): c(17) = a1(j17) Else GoTo 170
    a(17) = a1(j17)

    a(21) = Pr4 - a(17): If b(a(21)) = 0 Then b(a(21)) = a(21): c(21) = a(21) Else GoTo 210

    a(18) = s4 - a(17) - a(16) - a(15)
    If a(18) < a1(m1) Or a(18) > a1(m2) Then GoTo 180:
    If b1(a(18)) = 0 Then GoTo 180
    If b(a(18)) = 0 Then b(a(18)) = a(18): c(18) = a(18) Else GoTo 180

    a(22) = Pr4 - a(18): If b(a(22)) = 0 Then b(a(22)) = a(22): c(22) = a(22) Else GoTo 220
    
    n10 = n10 + 1
    If n10 = 3 Then

        a12(5) = a(15):   a12(6) = a(16):   a12(7) = a(17):   a12(8) = a(18):
        a12(137) = a(19): a12(138) = a(20): a12(139) = a(21): a12(140) = a(22):
    
        n31 = 15: n32 = 22: GoSub 950              'Remove used primes and store in a3()

        Erase b, c: GoTo 150
    
    Else
    
        a12(49) = a(15): a12(60) = a(19):
        a12(61) = a(16): a12(72) = a(20):
        a12(73) = a(17): a12(84) = a(21):
        a12(85) = a(18): a12(96) = a(22):
    
        Return
    
    End If

    b(c(22)) = 0: c(22) = 0
220 b(c(18)) = 0: c(18) = 0
180 b(c(21)) = 0: c(21) = 0
210 b(c(17)) = 0: c(17) = 0
170 Next j17

    b(c(20)) = 0: c(20) = 0
200 b(c(16)) = 0: c(16) = 0
160 Next j16

    b(c(19)) = 0: c(19) = 0
190 b(c(15)) = 0: c(15) = 0
150 Next j15

    fl1 = 0
    Return
    
'   Double Check Identical Numbers a12()

800 fl1 = 1
    For i1 = 1 To 144
       a20 = a12(i1): If a20 = 0 Then GoTo 810
       For i2 = (1 + i1) To 144
           If a20 = a12(i2) Then fl1 = 0: Return
       Next i2
810 Next i1
    Return

'   Remove used pairs from b1()

900 For i1 = n31 To n32
        b1(a(i1)) = 0
        b1(Pr4 - a(i1)) = 0 'For Non Symmetrcal Embedded Squares
    Next i1
    Return

'   Remove used pairs from b1()
'   Store  used primes temporarely in a3()

950 For i1 = n31 To n32
        b1(a(i1)) = 0
        n3 = n3 + 1: a3(n3) = a(i1)
    Next i1
    Return

'    Reassign primes a3() to available primes b1()
     
955  For i1 = 1 To n3
         b1(a3(i1)) = a3(i1)
     Next i1
     Erase a3: n3 = 0: n10 = 0
     Return

'    Print results (lines)

1640 Cells(n9, 122).Select
     For i1 = 1 To 144
         Cells(n9, i1).Value = a12(i1)
     Next i1
     Cells(n9, 145).Value = s12
     Cells(n9, 146).Value = j100
     Return

'    Print results (squares)

1650 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(s12)
    
     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

End Sub

Vorige Pagina About the Author