Vorige Pagina Volgende Pagina About the Author

' Generates Bordered Magic Cubes of order 6 (Prime Numbers)
' Part II: Anti Symmetric Magic Back Squares (c)

' Tested with Office 365 under Windows 10

Sub PrimeCubes61c()

Dim a1(724), a(100), b1(43291), b(43291), c(100), a6(36), C6(216)

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

    n2 = 0: n9 = 0: k1 = 1: k2 = 1
    ShtNm1 = "Pairs63"
    ShtNm2 = "TopSqrs6"
    
    Sheets("Klad1").Select
    
    t1 = Timer

For j100 = 79 To 98 
Cells(2, 1).Value = j100

'   Start Reading Data ShtNm2

    Mc6 = Sheets(ShtNm2).Cells(j100, 37).Value
    Rcrd1a = Sheets(ShtNm2).Cells(j100, 38).Value

'   Read Available Prime Numbers From Sheet ShtNm1

    GoSub 2015

'   Define Back Square

    Erase a                            'Clear Scratch Areas
    For i1 = 1 To 6
        a(i1) = C6(i1): a(i1 + 30) = C6(i1 + 180)
    Next i1
   
    t11 = Timer                                                         'Time Out

    For j29 = m2 To m1 Step -1                                          'a(29)
    If b(a1(j29)) = 0 Then b(a1(j29)) = a1(j29): c(29) = a1(j29) Else GoTo 290
    a(29) = a1(j29)

    a(65) = Pr6 - a(29): If b(a(65)) = 0 Then b(a(65)) = a(65): c(65) = a(65) Else GoTo 650
  
    For j22 = m2 To m1 Step -1                                          'a(22)
    If b(a1(j22)) = 0 Then b(a1(j22)) = a1(j22): c(22) = a1(j22) Else GoTo 220
    a(22) = a1(j22)

    a(58) = Pr6 - a(22): If b(a(58)) = 0 Then b(a(58)) = a(58): c(58) = a(58) Else GoTo 580

    For j15 = m1 To m2                                                  'a(15)
    If b(a1(j15)) = 0 Then b(a1(j15)) = a1(j15): c(15) = a1(j15) Else GoTo 150
    a(15) = a1(j15)

    a(51) = Pr6 - a(15): If b(a(51)) = 0 Then b(a(51)) = a(51): c(51) = a(51) Else GoTo 510
    
    a(8) = s6 - a(15) - a(22) - a(29) - a(1) - a(36)
    If a(8) < a1(m1) Or a(8) > a1(m2) Then GoTo 80
    If b1(a(8)) = 0 Then GoTo 80
    If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 80

    a(44) = Pr6 - a(8): If b(a(44)) = 0 Then b(a(44)) = a(44): c(44) = a(44) Else GoTo 440

    For j23 = m1 To m2                                                  'a(23)
    If b(a1(j23)) = 0 Then b(a1(j23)) = a1(j23): c(23) = a1(j23) Else GoTo 230
    a(23) = a1(j23)

    a(59) = Pr6 - a(23): If b(a(59)) = 0 Then b(a(59)) = a(59): c(59) = a(59) Else GoTo 590

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

    a(53) = Pr6 - a(17): If b(a(53)) = 0 Then b(a(53)) = a(53): c(53) = a(53) Else GoTo 530

    a(11) = s6 - a(17) - a(23) - a(29) - a(5) - a(35)
    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(47) = Pr6 - a(11): If b(a(47)) = 0 Then b(a(47)) = a(47): c(47) = a(47) Else GoTo 470

    For j26 = m1 To m2                                                  'a(26)
    If b(a1(j26)) = 0 Then b(a1(j26)) = a1(j26): c(26) = a1(j26) Else GoTo 260
    a(26) = a1(j26)

    a(62) = Pr6 - a(26): If b(a(62)) = 0 Then b(a(62)) = a(62): c(62) = a(62) Else GoTo 620

    For j21 = m1 To m2                                                  'a(21)
    If b(a1(j21)) = 0 Then b(a1(j21)) = a1(j21): c(21) = a1(j21) Else GoTo 210
    a(21) = a1(j21)

    a(57) = Pr6 - a(21): If b(a(57)) = 0 Then b(a(57)) = a(57): c(57) = a(57) Else GoTo 570

    a(16) = s6 - a(21) - a(26) - a(11) - a(6) - a(31)
    If a(16) < a1(m1) Or a(16) > a1(m2) Then GoTo 160
    If b1(a(16)) = 0 Then GoTo 160
    If b(a(16)) = 0 Then b(a(16)) = a(16): c(16) = a(16) Else GoTo 160

    a(52) = Pr6 - a(16): If b(a(52)) = 0 Then b(a(52)) = a(52): c(52) = a(52) Else GoTo 520

    For j20 = m1 To m2                                                  'a(20)
    If b(a1(j20)) = 0 Then b(a1(j20)) = a1(j20): c(20) = a1(j20) Else GoTo 200
    a(20) = a1(j20)

    a(56) = Pr6 - a(20): If b(a(56)) = 0 Then b(a(56)) = a(56): c(56) = a(56) Else GoTo 560

    a(14) = s6 - a(20) - a(26) - a(8) - a(2) - a(32)
    If a(14) < a1(m1) Or a(14) > a1(m2) Then GoTo 140
    If b1(a(14)) = 0 Then GoTo 140
    If b(a(14)) = 0 Then b(a(14)) = a(14): c(14) = a(14) Else GoTo 140

    a(50) = Pr6 - a(14): If b(a(50)) = 0 Then b(a(50)) = a(50): c(50) = a(50) Else GoTo 500

    For j28 = m1 To m2                                                  'a(28)
    If b(a1(j28)) = 0 Then b(a1(j28)) = a1(j28): c(28) = a1(j28) Else GoTo 280
    a(28) = a1(j28)

    a(64) = Pr6 - a(28): If b(a(64)) = 0 Then b(a(64)) = a(64): c(64) = a(64) Else GoTo 640

    a(10) = s6 - a(28) - a(16) - a(22) - a(4) - a(34)
    If a(10) < a1(m1) Or a(10) > a1(m2) Then GoTo 100
    If b1(a(10)) = 0 Then GoTo 100
    If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 100

    a(46) = Pr6 - a(10): If b(a(46)) = 0 Then b(a(46)) = a(46): c(46) = a(46) Else GoTo 460

    For j27 = m1 To m2                                                  'a(27)
    If b(a1(j27)) = 0 Then b(a1(j27)) = a1(j27): c(27) = a1(j27) Else GoTo 270
    a(27) = a1(j27)

    a(63) = Pr6 - a(27): If b(a(63)) = 0 Then b(a(63)) = a(63): c(63) = a(63) Else GoTo 630

    a(9) = s6 - a(27) - a(21) - a(15) - a(3) - a(33)
    If a(9) < a1(m1) Or a(9) > a1(m2) Then GoTo 90
    If b1(a(9)) = 0 Then GoTo 90
    If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 90

    a(45) = Pr6 - a(9): If b(a(45)) = 0 Then b(a(45)) = a(45): c(45) = a(45) Else GoTo 450

    For j30 = m1 To m2                                                  'a(30)
    If b(a1(j30)) = 0 Then b(a1(j30)) = a1(j30): c(30) = a1(j30) Else GoTo 300
    a(30) = a1(j30)

    a(66) = Pr6 - a(30): If b(a(66)) = 0 Then b(a(66)) = a(66): c(66) = a(66) Else GoTo 660

    a(25) = s6 - a(30) - a(27) - a(28) - a(26) - a(29)
    If a(25) < a1(m1) Or a(25) > a1(m2) Then GoTo 250
    If b1(a(25)) = 0 Then GoTo 250
    If b(a(25)) = 0 Then b(a(25)) = a(25): c(25) = a(25) Else GoTo 250

    a(61) = Pr6 - a(25): If b(a(61)) = 0 Then b(a(61)) = a(61): c(61) = a(61) Else GoTo 610

    For j24 = m1 To m2                                                  'a(24)
    If b(a1(j24)) = 0 Then b(a1(j24)) = a1(j24): c(24) = a1(j24) Else GoTo 240
    a(24) = a1(j24)

    a(60) = Pr6 - a(24): If b(a(60)) = 0 Then b(a(60)) = a(60): c(60) = a(60) Else GoTo 600

    a(19) = s6 - a(24) - a(20) - a(21) - a(23) - a(22)
    If a(19) < a1(m1) Or a(19) > a1(m2) Then GoTo 190
   
    If b1(a(19)) = 0 Then GoTo 190
    If b(a(19)) = 0 Then b(a(19)) = a(19): c(19) = a(19) Else GoTo 190

    a(55) = Pr6 - a(19): If b(a(55)) = 0 Then b(a(55)) = a(55): c(55) = a(55) Else GoTo 550

    For j18 = m1 To m2                                                  'a(18)
    If b(a1(j18)) = 0 Then b(a1(j18)) = a1(j18): c(18) = a1(j18) Else GoTo 180
    a(18) = a1(j18)

    a(54) = Pr6 - a(18): If b(a(54)) = 0 Then b(a(54)) = a(54): c(54) = a(54) Else GoTo 540

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

    a(49) = Pr6 - a(13): If b(a(49)) = 0 Then b(a(49)) = a(49): c(49) = a(49) Else GoTo 490

    a(12) = s6 - a(30) - a(18) - a(24) - a(6) - a(36)
    If a(12) < a1(m1) Or a(12) > a1(m2) Then GoTo 120
    If b1(a(12)) = 0 Then GoTo 120
    If b(a(12)) = 0 Then b(a(12)) = a(12): c(12) = a(12) Else GoTo 120
    
    a(48) = Pr6 - a(12): If b(a(48)) = 0 Then b(a(48)) = a(48): c(48) = a(48) Else GoTo 480
    
    a(7) = s6 - a(12) - a(9) - a(10) - a(11) - a(8)
    If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 70
    If b1(a(7)) = 0 Then GoTo 70
    If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 70

    a(43) = Pr6 - a(7): If b(a(43)) = 0 Then b(a(43)) = a(43): c(43) = a(43) Else GoTo 430


            GoSub 850                 'Exclude solutions with identical numbers (back check)
            If fl1 = 0 Then GoTo 5

''          n9 = n9 + 1: GoSub 1740   'Print results (selected numbers)
            n9 = n9 + 1: GoSub 1750   'Print results (squares)
            Erase b, c:  GoTo 1000    'Print only first Top Square

5

    b(c(43)) = 0: c(43) = 0
430 b(c(7)) = 0: c(7) = 0
70  b(c(48)) = 0: c(48) = 0
480 b(c(12)) = 0: c(12) = 0
120 b(c(49)) = 0: c(49) = 0
490 b(c(13)) = 0: c(13) = 0
130 b(c(54)) = 0: c(54) = 0
540 b(c(18)) = 0: c(18) = 0
180 Next j18

    b(c(55)) = 0: c(55) = 0
550 b(c(19)) = 0: c(19) = 0
190 b(c(60)) = 0: c(60) = 0
600 b(c(24)) = 0: c(24) = 0
240 Next j24

    b(c(61)) = 0: c(61) = 0
610 b(c(25)) = 0: c(25) = 0
250 b(c(66)) = 0: c(66) = 0
660 b(c(30)) = 0: c(30) = 0
300 Next j30

    b(c(45)) = 0: c(45) = 0
450 b(c(9)) = 0: c(9) = 0
90  b(c(63)) = 0: c(63) = 0
630 b(c(27)) = 0: c(27) = 0
270 Next j27
    
    b(c(46)) = 0: c(46) = 0
460 b(c(10)) = 0: c(10) = 0
100 b(c(64)) = 0: c(64) = 0
640 b(c(28)) = 0: c(28) = 0
280 Next j28

    b(c(50)) = 0: c(50) = 0
500 b(c(14)) = 0: c(14) = 0
140 b(c(56)) = 0: c(56) = 0
560 b(c(20)) = 0: c(20) = 0
200 Next j20

    b(c(52)) = 0: c(52) = 0
520 b(c(16)) = 0: c(16) = 0
160 b(c(57)) = 0: c(57) = 0
570 b(c(21)) = 0: c(21) = 0
210 Next j21

    b(c(62)) = 0: c(62) = 0
620 b(c(26)) = 0: c(26) = 0
260 Next j26

    b(c(47)) = 0: c(47) = 0
470 b(c(11)) = 0: c(11) = 0
110 b(c(53)) = 0: c(53) = 0
530 b(c(17)) = 0: c(17) = 0
170 Next j17

    b(c(59)) = 0: c(59) = 0
590 b(c(23)) = 0: c(23) = 0
230 Next j23

    b(c(44)) = 0: c(44) = 0
440 b(c(8)) = 0: c(8) = 0
80  b(c(51)) = 0: c(51) = 0
510 b(c(15)) = 0: c(15) = 0
150 Next j15

    b(c(58)) = 0: c(58) = 0
580 b(c(22)) = 0: c(22) = 0
220 Next j22

    b(c(65)) = 0: c(65) = 0
650 b(c(29)) = 0: c(29) = 0
290 Next j29

1000 Erase b1, b, c
     Next j100

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

End

'    Exclude solutions with identical numbers a()

850  fl1 = 1
     For j1 = 1 To 36
        a20 = a(j1): If a20 = 0 Then GoTo 855
        For j2 = (1 + j1) To 36
            If a20 = a(j2) Then fl1 = 0: Return
        Next j2
855  Next j1
     Return

'   Print results (selected numbers)

1740 For i1 = 1 To 36
        Cells(n9, i1).Value = a(i1)
     Next i1
     Cells(1, 39).Value = n9
     Cells(n9, 37).Value = s6
     Cells(n9, 38).Value = Rcrd1a      'Record
     Return

'   Print results, back square a()

1750 n2 = n2 + 1
     If n2 = 4 Then
          n2 = 1: k1 = k1 + 7: k2 = 1
     Else
          If n9 > 1 Then k2 = k2 + 7
     End If
     Cells(1, 1).Value = n9
     
     Cells(k1, k2 + 1).Font.Color = -4165632
     Cells(k1, k2 + 1).Value = s6
   
     i3 = 0
     For i1 = 1 To 6
         For i2 = 1 To 6
             i3 = i3 + 1
             Cells(k1 + i1, k2 + i2).Value = a(i3)
         Next i2
     Next i1
    
     Return

'   Fill Cube c6()

750
'   Top Square

    For i1 = 1 To 36
        C6(i1) = a6(i1)
    Next i1
    
'   Bottom Square
    
    C6(181) = Pr6 - C6(36): C6(182) = Pr6 - C6(32): C6(183) = Pr6 - C6(33):
    C6(184) = Pr6 - C6(34): C6(185) = Pr6 - C6(35): C6(186) = Pr6 - C6(31):
    C6(187) = Pr6 - C6(12): C6(188) = Pr6 - C6(8):  C6(189) = Pr6 - C6(9):
    C6(190) = Pr6 - C6(10): C6(191) = Pr6 - C6(11): C6(192) = Pr6 - C6(7):
    C6(193) = Pr6 - C6(18): C6(194) = Pr6 - C6(14): C6(195) = Pr6 - C6(15):
    C6(196) = Pr6 - C6(16): C6(197) = Pr6 - C6(17): C6(198) = Pr6 - C6(13):
    C6(199) = Pr6 - C6(24): C6(200) = Pr6 - C6(20): C6(201) = Pr6 - C6(21):
    C6(202) = Pr6 - C6(22): C6(203) = Pr6 - C6(23): C6(204) = Pr6 - C6(19):
    C6(205) = Pr6 - C6(30): C6(206) = Pr6 - C6(26): C6(207) = Pr6 - C6(27):
    C6(208) = Pr6 - C6(28): C6(209) = Pr6 - C6(29): C6(210) = Pr6 - C6(25):
    C6(211) = Pr6 - C6(6):  C6(212) = Pr6 - C6(2):  C6(213) = Pr6 - C6(3):
    C6(214) = Pr6 - C6(4):  C6(215) = Pr6 - C6(5):  C6(216) = Pr6 - C6(1):

    Return
    
'   Remove used primes and complements from available primes

950 For i1 = 1 To 36
        b1(a6(i1)) = 0
        b1(Pr6 - a6(i1)) = 0
    Next i1
    Return
    
'    Read Prime Numbers From sheet ShtNm1

2015 Pr6 = Sheets(ShtNm1).Cells(Rcrd1a, 1).Value    ' Pairsum
     nVar1 = Sheets(ShtNm1).Cells(Rcrd1a, 5).Value
     s4 = 2 * Pr6                                 ' MC4
     s6 = 3 * Pr6                                 ' Mc6
     
     Erase b1
     For j1 = 1 To nVar1
         x = Sheets(ShtNm1).Cells(Rcrd1a, 10 + j1).Value
         b1(x) = x
     Next j1
     pMax = Sheets(ShtNm1).Cells(Rcrd1a, 10 + nVar1).Value
    
'    Read Top Square 6 x 6
        
     For i1 = 1 To 36
         a6(i1) = Sheets(ShtNm2).Cells(j100, i1).Value
     Next i1
     GoSub 750      'Fill Top and Bottom c6()

     GoSub 950      'Remove used primes and complements from available primes

'    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

     Return

End Sub

Vorige Pagina Volgende Pagina About the Author