Vorige Pagina About the Author

' Generates Magic Cubes of order 3 for Prime Numbers

' Tested with Office 2007 under Windows 7

Sub PrimeCubes3()

Dim a1(623), a(27), b1(43291), b(43291), C(27)

y = MsgBox("Locked", vbCritical, "Routine Prien3")
End
    
    n1 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
    ShtNm1 = "Pairs3"

    Sheets("Klad1").Select

'   Generate Cubes
    
    t1 = Timer

For j100 = 43 To 742 ''2410

'   Define variables

    s1 = Sheets(ShtNm1).Cells(j100, 3).Value     ' MC3
    nVar1 = Sheets(ShtNm1).Cells(j100, 5).Value
    If nVar1 < 27 Then GoTo 100
    
'   Read Prime Numbers From sheet ShtNm1
    
    For i1 = 1 To nVar1
        a1(i1) = Sheets(ShtNm1).Cells(j100, 6 + i1).Value
    Next i1

    m1 = 1: m2 = nVar1

    Erase b1

    For i1 = m1 To m2
        b1(a1(i1)) = a1(i1)
    Next i1

a(14) = s1 / 3
If b1(a(14)) = 0 Then GoTo 100
b(a(14)) = a(14): C(14) = a(14)

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

a(1) = 2 * s1 / 3 - a(27)
If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 10
If b1(a(1)) = 0 Then GoTo 10
If b(a(1)) = 0 Then b(a(1)) = a(1): C(1) = a(1) Else GoTo 10

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

a(25) = s1 - a(26) - a(27)
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(3) = -s1 / 3 + a(26) + a(27)
If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 30
If b1(a(3)) = 0 Then GoTo 30
If b(a(3)) = 0 Then b(a(3)) = a(3): C(3) = a(3) Else GoTo 30
a(2) = 2 * s1 / 3 - a(26)
If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 20
If b1(a(2)) = 0 Then GoTo 20
If b(a(2)) = 0 Then b(a(2)) = a(2): C(2) = a(2) Else GoTo 20

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

a(21) = s1 - a(24) - a(27)
If a(21) < a1(m1) Or a(21) > a1(m2) Then GoTo 210
If b1(a(21)) = 0 Then GoTo 210
If b(a(21)) = 0 Then b(a(21)) = a(21): C(21) = a(21) Else GoTo 210
a(16) = s1 / 3 - a(24) + a(26)
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(12) = s1 / 3 + a(24) - a(26)
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(7) = -s1 / 3 + a(24) + a(27)
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(4) = 2 * s1 / 3 - a(24)
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

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

    a(22) = s1 - a(23) - a(24)
    If a(22) < a1(m1) Or a(22) > a1(m2) Then GoTo 220:
    If b1(a(22)) = 0 Then GoTo 220
    a(20) = s1 - a(23) - a(26)
    If a(20) < a1(m1) Or a(20) > a1(m2) Then GoTo 220:
    If b1(a(20)) = 0 Then GoTo 220
    a(19) = -s1 + a(23) + a(24) + a(26) + a(27)
    If a(19) < a1(m1) Or a(19) > a1(m2) Then GoTo 220:
    If b1(a(19)) = 0 Then GoTo 220
    a(18) = -2 * s1 / 3 + a(23) + a(24) + a(26)
    If a(18) < a1(m1) Or a(18) > a1(m2) Then GoTo 220:
    If b1(a(18)) = 0 Then GoTo 220
    a(17) = 4 * s1 / 3 - a(23) - 2 * a(26)
    If a(17) < a1(m1) Or a(17) > a1(m2) Then GoTo 220:
    If b1(a(17)) = 0 Then GoTo 220
    a(15) = 4 * s1 / 3 - a(23) - 2 * a(24)
    If a(15) < a1(m1) Or a(15) > a1(m2) Then GoTo 220:
    If b1(a(15)) = 0 Then GoTo 220
    a(13) = -2 * s1 / 3 + a(23) + 2 * a(24)
    If a(13) < a1(m1) Or a(13) > a1(m2) Then GoTo 220:
    If b1(a(13)) = 0 Then GoTo 220
    a(11) = -2 * s1 / 3 + a(23) + 2 * a(26)
    If a(11) < a1(m1) Or a(11) > a1(m2) Then GoTo 220:
    If b1(a(11)) = 0 Then GoTo 220
    a(10) = 4 * s1 / 3 - a(23) - a(24) - a(26)
    If a(10) < a1(m1) Or a(10) > a1(m2) Then GoTo 220:
    If b1(a(10)) = 0 Then GoTo 220
    a(9) = 5 * s1 / 3 - a(23) - a(24) - a(26) - a(27)
    If a(9) < a1(m1) Or a(9) > a1(m2) Then GoTo 220:
    If b1(a(9)) = 0 Then GoTo 220
    a(8) = -s1 / 3 + a(23) + a(26)
    If a(8) < a1(m1) Or a(8) > a1(m2) Then GoTo 220:
    If b1(a(8)) = 0 Then GoTo 220
    a(6) = -s1 / 3 + a(23) + a(24)
    If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 220:
    If b1(a(6)) = 0 Then GoTo 220
    a(5) = 2 * s1 / 3 - a(23)
    If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 220:
    If b1(a(5)) = 0 Then GoTo 220

'                         Exclude solutions with identical numbers

                          GoSub 800: If fl1 = 0 Then GoTo 220

'                         n9 = n9 + 1: GoSub 640 'Print results   (selected numbers)
                          n9 = n9 + 1: GoSub 650 'Print results   (cubes)
                          Erase b, C: GoTo 100   'Print only first cube

   
220 b(C(23)) = 0: C(23) = 0
230 Next j23

    b(C(4)) = 0: C(4) = 0
40  b(C(7)) = 0: C(7) = 0
70  b(C(12)) = 0: C(12) = 0
120 b(C(16)) = 0: C(16) = 0
160 b(C(21)) = 0: C(21) = 0
210 b(C(24)) = 0: C(24) = 0
240 Next j24

    b(C(2)) = 0: C(2) = 0
20  b(C(3)) = 0: C(3) = 0
30  b(C(25)) = 0: C(25) = 0
250 b(C(26)) = 0: C(26) = 0
260 Next j26

    b(C(1)) = 0: C(1) = 0
10  b(C(27)) = 0: C(27) = 0
270 Next j27
    
    b(C(14)) = 0: C(14) = 0
    n10 = 0

100 Next j100

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

End

'   Print results (selected numbers)

640 Cells(n9, 9).Select
    For i1 = 1 To 27
        Cells(n9, i1).Value = a(i1)
    Next i1
    Return

'   Print results (planes 1, 2 and 3)

650 n2 = n2 + 1
    If n2 = 7 Then
        n2 = 1: k1 = k1 + 12: k2 = 1
    Else
        If n9 > 1 Then k2 = k2 + 4
    End If
        
    Cells(k1, k2 + 1).Select
    Cells(k1, k2 + 1).Font.Color = -4165632
   
    Cells(k1, k2 + 1).Value = "MC = " + CStr(s1)
        
    For i0 = 1 To 3
        i3 = (i0 - 1) * 9
        For i1 = 1 To 3
            For i2 = 1 To 3
                i3 = i3 + 1
                Cells(k1 + i1 + (i0 - 1) * 4, k2 + i2).Value = a(i3)
            Next i2
        Next i1
    Next i0
    
    Return

'   Exclude solutions with identical numbers

800 fl1 = 1
    For j1 = 1 To 27
       a2 = a(j1)
       For j2 = (1 + j1) To 27
           If a2 = a(j2) Then fl1 = 0: Return
       Next j2
    Next j1
    Return

End Sub

Vorige Pagina About the Author