Vorige Pagina About the Author

' Constructs 9 x 9 Associated Magic Squares (Distinct Prime Numbers)
' Associated Magic Diamond Inlays Order 4 and 5
' Tested with Office 2007 under Windows 7

Sub Prime9f4()

    Dim a1(2448), a(25), a9(81), b1(43300), b(43300), c(81)
 
    y = MsgBox("Locked", vbCritical, "Routine Priem9f4")
    End

    Sheets("Klad1").Select

    n5 = 0: n9 = 0: k1 = 1: k2 = 1
    ShtNm1 = "Pairs7"
    ShtNm2 = "Lines5"
    
    t1 = Timer

    For j101 = 116 To 116
    j100 = Sheets(ShtNm2).Cells(j101, 27).Value
    MC5 = Sheets(ShtNm2).Cells(j101, 26).Value
    
'       Read Pairs

        Pr3 = Sheets(ShtNm1).Cells(j100, 1).Value        'PairSum
        Cntr3 = Sheets(ShtNm1).Cells(j100, 6).Value      'Center Element
        s5 = 5 * Cntr3                                   'MC5
        s3 = 3 * Cntr3                                   'MC3
        s4 = 2 * Pr3                                     'MC4
        s9 = 9 * Cntr3                                   'MC9
        nvar = Sheets(ShtNm1).Cells(j100, 9).Value

        If nvar < 155 Then GoTo 1000
        
        If MC5 <> s5 Then
                y = MsgBox("Conflict in Data", vbCritical, "Read " + ShtNm2)
                End
        End If

        Erase b1
        For j1 = 1 To nvar
            x = Sheets(ShtNm1).Cells(j100, 9 + j1).Value
            a1(j1) = x: b1(x) = x
        Next j1
        pMax = Sheets(ShtNm1).Cells(j100, 9 + nvar).Value
        
'       Read Diamond 5 x 5 (Associated Magic Square)
        
        For i1 = 1 To 25
            a(i1) = Sheets(ShtNm2).Cells(j101, i1).Value
        Next i1
        n10 = 0: n53 = 25: GoSub 910      'Remove used primes from available primes
        
        Erase a9

                                            a9(5) = a(5):
                                   a9(13) = a(4):    a9(15) = a(10):
                          a9(21) = a(3):    a9(23) = a(9):    a9(25) = a(15):
                 a9(29) = a(2):    a9(31) = a(8):    a9(33) = a(14):   a9(35) = a(20):
        a9(37) = a(1):    a9(39) = a(7):    a9(41) = a(13):   a9(43) = a(19):   a9(45) = a(25):
                 a9(47) = a(6):    a9(49) = a(12):   a9(51) = a(18):   a9(53) = a(24):
                          a9(57) = a(11):   a9(59) = a(17):   a9(61) = a(23):
                                   a9(67) = a(16):   a9(69) = a(22):
                                            a9(77) = a(21):

        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 Diamond 4 x 4 (Associated Magic Square)

        For j44 = m1 To m2                                          'a9(44)
        If b1(a1(j44)) = 0 Then GoTo 440
        If b(a1(j44)) = 0 Then b(a1(j44)) = a1(j44): c(44) = a1(j44) Else GoTo 440
        a9(44) = a1(j44)
       
        a9(38) = Pr3 - a9(44): If b(a9(38)) = 0 Then b(a9(38)) = a9(38): c(38) = a9(38) Else GoTo 380
         
        For j52 = m1 To m2                                          'a9(52)
        If b1(a1(j52)) = 0 Then GoTo 520
        If b(a1(j52)) = 0 Then b(a1(j52)) = a1(j52): c(52) = a1(j52) Else GoTo 520
        a9(52) = a1(j52)
        
        a9(30) = Pr3 - a9(52): If b(a9(30)) = 0 Then b(a9(30)) = a9(30): c(30) = a9(30) Else GoTo 300
        
        For j60 = m1 To m2                                          'a9(60)
        If b1(a1(j60)) = 0 Then GoTo 600
        If b(a1(j60)) = 0 Then b(a1(j60)) = a1(j60): c(60) = a1(j60) Else GoTo 600
        a9(60) = a1(j60)
        
        a9(68) = s4 - a9(60) - a9(52) - a9(44)
        If a9(68) < a1(m1) Or a9(68) > a1(m2) Then GoTo 680
        If b1(a9(68)) = 0 Then GoTo 680
        If b(a9(68)) = 0 Then b(a9(68)) = a9(68): c(68) = a9(68) Else GoTo 680
        
        a9(22) = Pr3 - a9(60): If b(a9(22)) = 0 Then b(a9(22)) = a9(22): c(22) = a9(22) Else GoTo 220
        a9(14) = Pr3 - a9(68): If b(a9(14)) = 0 Then b(a9(14)) = a9(14): c(14) = a9(14) Else GoTo 140

        For j34 = m1 To m2                                          'a9(34)
        If b1(a1(j34)) = 0 Then GoTo 340
        If b(a1(j34)) = 0 Then b(a1(j34)) = a1(j34): c(34) = a1(j34) Else GoTo 340
        a9(34) = a1(j34)

        a9(42) = s4 - a9(34) - a9(52) - a9(44)
        If a9(42) < a1(m1) Or a9(42) > a1(m2) Then GoTo 420
        If b1(a9(42)) = 0 Then GoTo 420
        If b(a9(42)) = 0 Then b(a9(42)) = a9(42): c(42) = a9(42) Else GoTo 420
        
        a9(50) = s4 - a9(34) - a9(60) - a9(44)
        If a9(50) < a1(m1) Or a9(50) > a1(m2) Then GoTo 500
        If b1(a9(50)) = 0 Then GoTo 500
        If b(a9(50)) = 0 Then b(a9(50)) = a9(50): c(50) = a9(50) Else GoTo 500
        
        a9(58) = s4 - a9(50) - a9(42) - a9(34)
        If a9(58) < a1(m1) Or a9(58) > a1(m2) Then GoTo 580
        If b1(a9(58)) = 0 Then GoTo 580
        If b(a9(58)) = 0 Then b(a9(58)) = a9(58): c(58) = a9(58) Else GoTo 580
    
        a9(24) = Pr3 - a9(58): If b(a9(24)) = 0 Then b(a9(24)) = a9(24): c(24) = a9(24) Else GoTo 240
        a9(32) = Pr3 - a9(50): If b(a9(32)) = 0 Then b(a9(32)) = a9(32): c(32) = a9(32) Else GoTo 320
        a9(40) = Pr3 - a9(42): If b(a9(40)) = 0 Then b(a9(40)) = a9(40): c(40) = a9(40) Else GoTo 400
        a9(48) = Pr3 - a9(34): If b(a9(48)) = 0 Then b(a9(48)) = a9(48): c(48) = a9(48) Else GoTo 480

                GoSub 2000                          'Complete Border (1)
                If fl2 = 0 Then GoTo 5              'No Border
                
                GoSub 800                           'Double Check Identical Integers a9()
                If fl1 = 1 Then
                      n9 = n9 + 1: GoSub 650        'Print Composed Squares a9()
                End If
                Erase b, c: GoTo 1000               'Print only first Square

5
    
    b(c(48)) = 0: c(48) = 0
480 b(c(40)) = 0: c(40) = 0
400 b(c(32)) = 0: c(32) = 0
320 b(c(24)) = 0: c(24) = 0
240 b(c(58)) = 0: c(58) = 0
580 b(c(50)) = 0: c(50) = 0
500 b(c(42)) = 0: c(42) = 0
420 b(c(34)) = 0: c(34) = 0
340 Next j34
    
    b(c(14)) = 0: c(14) = 0
140 b(c(22)) = 0: c(22) = 0
220 b(c(68)) = 0: c(68) = 0
680 b(c(60)) = 0: c(60) = 0
600 Next j60

    b(c(30)) = 0: c(30) = 0
300 b(c(52)) = 0: c(52) = 0
520 Next j52

    b(c(38)) = 0: c(38) = 0
380 b(c(44)) = 0: c(44) = 0
440 Next j44

     Erase b1, b, c
1000 Next j101

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

End

'    Complete Border

2000 fl2 = 1

     For j81 = m1 To m2                                          'a9(81)
     If b1(a1(j81)) = 0 Then GoTo 2810
     If b(a1(j81)) = 0 Then b(a1(j81)) = a1(j81): c(81) = a1(j81) Else GoTo 2810
     a9(81) = a1(j81)

     a9(1) = Pr3 - a9(81): If b(a9(1)) = 0 Then b(a9(1)) = a9(1): c(1) = a9(1) Else GoTo 2010

     For j80 = m1 To m2                                          'a9(80)
     If b1(a1(j80)) = 0 Then GoTo 2800
     If b(a1(j80)) = 0 Then b(a1(j80)) = a1(j80): c(80) = a1(j80) Else GoTo 2800
     a9(80) = a1(j80)

     a9(79) = s3 - a9(80) - a9(81)
     If a9(79) < a1(m1) Or a9(79) > a1(m2) Then GoTo 2790
     If b1(a9(79)) = 0 Then GoTo 2790
     If b(a9(79)) = 0 Then b(a9(79)) = a9(79): c(79) = a9(79) Else GoTo 2790

     a9(3) = Pr3 - a9(79): If b(a9(3)) = 0 Then b(a9(3)) = a9(3): c(3) = a9(3) Else GoTo 2030
     a9(2) = Pr3 - a9(80): If b(a9(2)) = 0 Then b(a9(2)) = a9(2): c(2) = a9(2) Else GoTo 2020

     For j72 = m1 To m2                                          'a9(72)
     If b1(a1(j72)) = 0 Then GoTo 2720
     If b(a1(j72)) = 0 Then b(a1(j72)) = a1(j72): c(72) = a1(j72) Else GoTo 2720
     a9(72) = a1(j72)

     a9(63) = s3 - a9(72) - a9(81)
     If a9(63) < a1(m1) Or a9(63) > a1(m2) Then GoTo 2630
     If b1(a9(63)) = 0 Then GoTo 2630
     If b(a9(63)) = 0 Then b(a9(63)) = a9(63): c(63) = a9(63) Else GoTo 2630

     a9(19) = Pr3 - a9(63): If b(a9(19)) = 0 Then b(a9(19)) = a9(19): c(19) = a9(19) Else GoTo 2190
     a9(10) = Pr3 - a9(72): If b(a9(10)) = 0 Then b(a9(10)) = a9(10): c(10) = a9(10) Else GoTo 2100

     For j71 = m1 To m2                                          'a9(71)
     If b1(a1(j71)) = 0 Then GoTo 2710
     If b(a1(j71)) = 0 Then b(a1(j71)) = a1(j71): c(71) = a1(j71) Else GoTo 2710
     a9(71) = a1(j71)

     a9(70) = s3 - a9(71) - a9(72)
     If a9(70) < a1(m1) Or a9(70) > a1(m2) Then GoTo 2700
     If b1(a9(70)) = 0 Then GoTo 2700
     If b(a9(70)) = 0 Then b(a9(70)) = a9(70): c(70) = a9(70) Else GoTo 2700

     a9(62) = s3 - a9(71) - a9(80)
     If a9(62) < a1(m1) Or a9(62) > a1(m2) Then GoTo 2620
     If b1(a9(62)) = 0 Then GoTo 2620
     If b(a9(62)) = 0 Then b(a9(62)) = a9(62): c(62) = a9(62) Else GoTo 2620

     a9(20) = Pr3 - a9(62): If b(a9(20)) = 0 Then b(a9(20)) = a9(20): c(20) = a9(20) Else GoTo 2200
     a9(12) = Pr3 - a9(70): If b(a9(12)) = 0 Then b(a9(12)) = a9(12): c(12) = a9(12) Else GoTo 2120
     a9(11) = Pr3 - a9(71): If b(a9(11)) = 0 Then b(a9(11)) = a9(11): c(11) = a9(11) Else GoTo 2110

     For j78 = m1 To m2
     If b1(a1(j78)) = 0 Then GoTo 2780
     If b(a1(j78)) = 0 Then b(a1(j78)) = a1(j78): c(78) = a1(j78) Else GoTo 2780
     a9(78) = a1(j78)
    
     a9(4) = Pr3 - a9(78): If b(a9(4)) = 0 Then b(a9(4)) = a9(4): c(4) = a9(4) Else GoTo 2040
    
     a9(76) = s9 - a9(4) - a9(13) - a9(22) - a9(31) - a9(40) - a9(49) - a9(58) - a9(67)
     If a9(76) < a1(m1) Or a9(76) > a1(m2) Then GoTo 2760
     If b1(a9(76)) = 0 Then GoTo 2760
     If b(a9(76)) = 0 Then b(a9(76)) = a9(76): c(76) = a9(76) Else GoTo 2760
    
     a9(6) = Pr3 - a9(76): If b(a9(6)) = 0 Then b(a9(6)) = a9(6): c(6) = a9(6) Else GoTo 2060

     For j66 = m1 To m2
     If b1(a1(j66)) = 0 Then GoTo 2660
     If b(a1(j66)) = 0 Then b(a1(j66)) = a1(j66): c(66) = a1(j66) Else GoTo 2660
     a9(66) = a1(j66)

     a9(16) = Pr3 - a9(66): If b(a9(16)) = 0 Then b(a9(16)) = a9(16): c(16) = a9(16) Else GoTo 2160

     a9(75) = s9 - a9(3) - a9(12) - a9(21) - a9(30) - a9(39) - a9(48) - a9(57) - a9(66)
     If a9(75) < a1(m1) Or a9(75) > a1(m2) Then GoTo 2750
     If b1(a9(75)) = 0 Then GoTo 2750
     If b(a9(75)) = 0 Then b(a9(75)) = a9(75): c(75) = a9(75) Else GoTo 2750

     a9(7) = Pr3 - a9(75): If b(a9(7)) = 0 Then b(a9(7)) = a9(7): c(7) = a9(7) Else GoTo 2070

     For j56 = m1 To m2
     If b1(a1(j56)) = 0 Then GoTo 2560
     If b(a1(j56)) = 0 Then b(a1(j56)) = a1(j56): c(56) = a1(j56) Else GoTo 2560
     a9(56) = a1(j56)

     a9(26) = Pr3 - a9(56): If b(a9(26)) = 0 Then b(a9(26)) = a9(26): c(26) = a9(26) Else GoTo 2260

     a9(55) = s9 - a9(56) - a9(57) - a9(58) - a9(59) - a9(60) - a9(61) - a9(62) - a9(63)
     If a9(55) < a1(m1) Or a9(55) > a1(m2) Then GoTo 2550
     If b1(a9(55)) = 0 Then GoTo 2550
     If b(a9(55)) = 0 Then b(a9(55)) = a9(55): c(55) = a9(55) Else GoTo 2550

     a9(27) = Pr3 - a9(55): If b(a9(27)) = 0 Then b(a9(27)) = a9(27): c(27) = a9(27) Else GoTo 2270

     For j65 = m1 To m2
     If b1(a1(j65)) = 0 Then GoTo 2650
     If b(a1(j65)) = 0 Then b(a1(j65)) = a1(j65): c(65) = a1(j65) Else GoTo 2650
     a9(65) = a1(j65)

     a9(17) = Pr3 - a9(65): If b(a9(17)) = 0 Then b(a9(17)) = a9(17): c(17) = a9(17) Else GoTo 2170

     a9(64) = s9 - a9(65) - a9(66) - a9(67) - a9(68) - a9(69) - a9(70) - a9(71) - a9(72)
     If a9(64) < a1(m1) Or a9(64) > a1(m2) Then GoTo 2640
     If b1(a9(64)) = 0 Then GoTo 2640
     If b(a9(64)) = 0 Then b(a9(64)) = a9(64): c(64) = a9(64) Else GoTo 2640

     a9(18) = Pr3 - a9(64): If b(a9(18)) = 0 Then b(a9(18)) = a9(18): c(18) = a9(18) Else GoTo 2180

     a9(74) = s9 - a9(2) - a9(11) - a9(20) - a9(29) - a9(38) - a9(47) - a9(56) - a9(65)
     If a9(74) < a1(m1) Or a9(74) > a1(m2) Then GoTo 2740
     If b1(a9(74)) = 0 Then GoTo 2740
     If b(a9(74)) = 0 Then b(a9(74)) = a9(74): c(74) = a9(74) Else GoTo 2740
     
     a9(8) = Pr3 - a9(74): If b(a9(8)) = 0 Then b(a9(8)) = a9(8): c(8) = a9(8) Else GoTo 2080

     a9(73) = s9 - a9(74) - a9(75) - a9(76) - a9(77) - a9(78) - a9(79) - a9(80) - a9(81)
     If a9(73) < a1(m1) Or a9(73) > a1(m2) Then GoTo 2730
     If b1(a9(73)) = 0 Then GoTo 2730
     If b(a9(73)) = 0 Then b(a9(73)) = a9(73): c(73) = a9(73) Else GoTo 2730
         
     a9(9) = Pr3 - a9(73): If b(a9(9)) = 0 Then b(a9(9)) = a9(9): c(9) = a9(9) Else GoTo 2090

     a9(54) = (7*s9-4*a9(55)-5*a9(56)-8*a9(64)-9*a9(65)-10*a9(66)-10*a9(70)-a9(74)-2*a9(75)-3*a9(76)+12*a9(77)-5*a9(78)-10*a9(79))/8
     If a9(54) < a1(m1) Or a9(54) > a1(m2) Or CInt(a9(54)) <> a9(54) Then GoTo 2540
     If b1(a9(54)) = 0 Then GoTo 2540
     If b(a9(54)) = 0 Then b(a9(54)) = a9(54): c(54) = a9(54) Else GoTo 2540

     a9(28) = Pr3 - a9(54): If b(a9(28)) = 0 Then b(a9(28)) = a9(28): c(28) = a9(28) Else GoTo 2280

     a9(46) = s9 - a9(47) - a9(48) - a9(49) - a9(50) - a9(51) - a9(52) - a9(53) - a9(54)
     If a9(46) < a1(m1) Or a9(46) > a1(m2) Then GoTo 2460
     If b1(a9(46)) = 0 Then GoTo 2460
     If b(a9(46)) = 0 Then b(a9(46)) = a9(46): c(46) = a9(46) Else GoTo 2460
    
     a9(36) = Pr3 - a9(46): If b(a9(36)) = 0 Then b(a9(36)) = a9(36): c(36) = a9(36) Else GoTo 2360
        
            fl2 = 1: Return

     b(c(36)) = 0: c(36) = 0
2360 b(c(46)) = 0: c(46) = 0
2460 b(c(28)) = 0: c(28) = 0
2280 b(c(54)) = 0: c(54) = 0
2540

     b(c(9)) = 0: c(9) = 0
2090 b(c(73)) = 0: c(73) = 0
2730 b(c(8)) = 0: c(8) = 0
2080 b(c(74)) = 0: c(74) = 0
2740 b(c(18)) = 0: c(18) = 0
2180 b(c(64)) = 0: c(64) = 0
2640 b(c(17)) = 0: c(17) = 0
2170 b(c(65)) = 0: c(65) = 0
2650 Next j65

     b(c(27)) = 0: c(27) = 0
2270 b(c(55)) = 0: c(55) = 0
2550 b(c(26)) = 0: c(26) = 0
2260 b(c(56)) = 0: c(56) = 0
2560 Next j56

     b(c(7)) = 0: c(7) = 0
2070 b(c(75)) = 0: c(75) = 0
2750 b(c(16)) = 0: c(16) = 0
2160 b(c(66)) = 0: c(66) = 0
2660 Next j66

     b(c(6)) = 0: c(6) = 0
2060 b(c(76)) = 0: c(76) = 0
2760 b(c(4)) = 0: c(4) = 0
2040 b(c(78)) = 0: c(78) = 0
2780 Next j78

     b(c(11)) = 0: c(11) = 0
2110 b(c(12)) = 0: c(12) = 0
2120 b(c(20)) = 0: c(20) = 0
2200 b(c(62)) = 0: c(62) = 0
2620 b(c(70)) = 0: c(70) = 0
2700 b(c(71)) = 0: c(71) = 0
2710 Next j71

     b(c(10)) = 0: c(10) = 0
2100 b(c(19)) = 0: c(19) = 0
2190 b(c(63)) = 0: c(63) = 0
2630 b(c(72)) = 0: c(72) = 0
2720 Next j72

     b(c(2)) = 0: c(2) = 0
2020 b(c(3)) = 0: c(3) = 0
2030 b(c(79)) = 0: c(79) = 0
2790 b(c(80)) = 0: c(80) = 0
2800 Next j80

     b(c(1)) = 0: c(1) = 0
2010 b(c(81)) = 0: c(81) = 0
2810 Next j81
    
    fl2 = 0
    Return

'    Print results (squares)

650  n5 = n5 + 1
     If n5 = 3 Then
         n5 = 1: k1 = k1 + 10: k2 = 1
     Else
         If n9 > 1 Then k2 = k2 + 10
     End If
     
     Cells(k1, k2 + 1).Select
     Cells(k1, k2 + 1).Font.Color = -4165632
     Cells(k1, k2 + 1).Value = "MC = " + CStr(s9)

     i3 = 0
     For i1 = 1 To 9
         For i2 = 1 To 9
             i3 = i3 + 1
             Cells(k1 + i1, k2 + i2).Value = a9(i3)
         Next i2
     Next i1
    
     Return

'   Double Check Identical Numbers a9()

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

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

910 For i1 = 1 To n53
         b1(a(i1)) = 0
    Next i1
    Return

End Sub

Vorige Pagina About the Author