Vorige Pagina About the Author

' Constructs 10 x 10 Magic Squares with Non Overlapping Subsquares (Distinct Prime Numbers):
' - Reads Prime Number (Symmetrical) Magic Squares (6 x 6)
' - Generates additional Prime Number Pan Magic Squares (4 x 4)
' - Completes the 10 x 10 Magic Squares

' Tested with Office 2007 under Windows 7

Sub Priem10a2()

    Dim a1(1944), a(100), b1(43300), b(43300), c(100)
    Dim a4(16), b4(16), c4(16), d6(36)               'Sub Squares

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

    n2 = 0: n3 = 0: k1 = 1: k2 = 1: n9 = 0
    Sht1 = "Pairs8"

'   Generate Squares

    Sheets("Klad1").Select
    
    t1 = Timer
    
For j100 = 2 To 7 ''9 To 88

'   Start Reading Data
    
    Rcrd1 = Sheets("Lines6").Cells(j100, 38).Value
    MC6 = Sheets("Lines6").Cells(j100, 37).Value
    MC10 = 5 * MC6 / 3
    
'   Read Prime Numbers used for PM6

    For i1 = 1 To 36
        a(i1) = Sheets("Lines6").Cells(j100, i1).Value
    Next i1
    
    GoSub 2010              'Read Prime Numbers From Sheet Sht1
    n8 = 36: GoSub 950      'Remove used primes from available primes
    
    For i1 = 1 To 36: d6(i1) = a(i1): Next i1
  
    For j16 = m1 To m2                                          'a(16)
    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)
    
    For j15 = m1 To m2                                          'a(15)
    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)
    
    For j14 = m1 To m2                                          'a(14)
    If b1(a1(j14)) = 0 Then GoTo 140
    If b(a1(j14)) = 0 Then b(a1(j14)) = a1(j14): c(14) = a1(j14) Else GoTo 140
    a(14) = a1(j14)
    
    a(13) = s1 - a(14) - a(15) - a(16)
    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
    
    For j12 = m1 To m2                                          'a(12)
    If b1(a1(j12)) = 0 Then GoTo 120
    If b(a1(j12)) = 0 Then b(a1(j12)) = a1(j12): c(12) = a1(j12) Else GoTo 120
    a(12) = a1(j12)
    
    a(11) = s1 - a(12) - a(15) - a(16)
    If a(11) < a1(m1) Or a(11) > a1(m2) Then GoTo 70
    If b1(a(11)) = 0 Then GoTo 70
    
    a(10) = a(12) - a(14) + a(16)
    If a(10) < a1(m1) Or a(10) > a1(m2) Then GoTo 70
    If b1(a(10)) = 0 Then GoTo 70
    
    a(9) = -a(12) + a(14) + a(15)
    If a(9) < a1(m1) Or a(9) > a1(m2) Then GoTo 70
    If b1(a(9)) = 0 Then GoTo 70
    
    a(8) = 0.5 * s1 - a(14)
    If a(8) < a1(m1) Or a(8) > a1(m2) Then GoTo 70
    If b1(a(8)) = 0 Then GoTo 70
    
    a(7) = -0.5 * s1 + a(14) + a(15) + a(16)
    If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 70:
    If b1(a(7)) = 0 Then GoTo 70
    
    a(6) = 0.5 * s1 - a(16)
    If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 70:
    If b1(a(6)) = 0 Then GoTo 70
    
    a(5) = 0.5 * s1 - a(15)
    If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 70:
    If b1(a(5)) = 0 Then GoTo 70
    
    a(4) = 0.5 * s1 - a(12) + a(14) - a(16)
    If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 70:
    If b1(a(4)) = 0 Then GoTo 70
    
    a(3) = 0.5 * s1 + a(12) - a(14) - a(15)
    If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 70:
    If b1(a(3)) = 0 Then GoTo 70
    
    a(2) = 0.5 * s1 - a(12)
    If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 70:
    If b1(a(2)) = 0 Then GoTo 70
    
    a(1) = -0.5 * s1 + a(12) + a(15) + a(16)
    If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 70:
    If b1(a(1)) = 0 Then GoTo 70
    
'                 Exclude solutions with identical numbers (PM4)
    
                  n8 = 16: GoSub 800: If fl1 = 0 Then GoTo 70
    
                  n10 = n10 + 1
                  Select Case n10
                         Case 1: n8 = 16: For i1 = 1 To 16: a4(i1) = a(i1): Next i1: GoSub 950
                         Case 2: n8 = 16: For i1 = 1 To 16: b4(i1) = a(i1): Next i1: GoSub 950
                         Case 3: n8 = 16: For i1 = 1 To 16: c4(i1) = a(i1): Next i1: GoSub 950
                  End Select

                  If n10 = 3 Then
                     GoSub 600                        'Compose Main Square

                     GoSub 1200                       'Complete Main Square
                     If fl2 = 1 Then
                        n8 = 100: GoSub 800           'Double Check Identical Integers
                        If fl1 = 1 Then
                           n9 = n9 + 1: GoSub 1650    'Print Composed Square
                        End If
                     End If
                     Erase b, c: n10 = 0: GoTo 10
                  Else
                     Erase b, c: GoTo 160             'Continue search for next Sub Square
                  End If
    
70  b(c(12)) = 0: c(12) = 0
120 Next j12

    b(c(13)) = 0: c(13) = 0
130 b(c(14)) = 0: c(14) = 0
140 Next j14
    b(c(15)) = 0: c(15) = 0
150 Next j15
    b(c(16)) = 0: c(16) = 0
160 Next j16

    n10 = 0
10  Next j100

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

End

'    Compose Main Square

600  Erase a
'    Symmetric Sub Square 6 x 6

     a(45) = d6(1):  a(46) = d6(2):  a(47) = d6(3):  a(48) = d6(4):  a(49) = d6(5):  a(50) = d6(6):
     a(55) = d6(7):  a(56) = d6(8):  a(57) = d6(9):  a(58) = d6(10): a(59) = d6(11): a(60) = d6(12):
     a(65) = d6(13): a(66) = d6(14): a(67) = d6(15): a(68) = d6(16): a(69) = d6(17): a(70) = d6(18):
     a(75) = d6(19): a(76) = d6(20): a(77) = d6(21): a(78) = d6(22): a(79) = d6(23): a(80) = d6(24):
     a(85) = d6(25): a(86) = d6(26): a(87) = d6(27): a(88) = d6(28): a(89) = d6(29): a(90) = d6(30):
     a(95) = d6(31): a(96) = d6(32): a(97) = d6(33): a(98) = d6(34): a(99) = d6(35): a(100) = d6(36):

'    Pan Magic Sub Squares  4 x 4

     a(51) = a4(1):  a(52) = a4(2):  a(53) = a4(3):  a(54) = a4(4):
     a(61) = a4(5):  a(62) = a4(6):  a(63) = a4(7):  a(64) = a4(8):
     a(71) = a4(9):  a(72) = a4(10): a(73) = a4(11): a(74) = a4(12):
     a(81) = a4(13): a(82) = a4(14): a(83) = a4(15): a(84) = a4(16):
    
     a(1) = b4(1):   a(2) = b4(2):   a(3) = b4(3):   a(4) = b4(4):
     a(11) = b4(5):  a(12) = b4(6):  a(13) = b4(7):  a(14) = b4(8):
     a(21) = b4(9):  a(22) = b4(10): a(23) = b4(11): a(24) = b4(12):
     a(31) = b4(13): a(32) = b4(14): a(33) = b4(15): a(34) = b4(16):
    
     a(6) = c4(1):   a(7) = c4(2):   a(8) = c4(3):   a(9) = c4(4):
     a(16) = c4(5):  a(17) = c4(6):  a(18) = c4(7):  a(19) = c4(8):
     a(26) = c4(9):  a(27) = c4(10): a(28) = c4(11): a(29) = c4(12):
     a(36) = c4(13): a(37) = c4(14): a(38) = c4(15): a(39) = c4(16):

     Return
     
'    Exclude solutions with identical numbers a()

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

'    Remove used primes from available primes

950  For j1 = 1 To n8
         b1(a(j1)) = 0
     Next j1

'    Restore available primes in a1()

     n20 = 0
     For j1 = 1 To a20
         If b1(j1) <> 0 Then
             n20 = n20 + 1
             a1(n20) = b1(j1)
         End If
     Next j1
     m1 = 1: m2 = n20
     Return
     
'    Complete Main Square

1200 fl2 = 1
     s2 = a(82) + a(73) + a(64) + a(55) + a(46) + a(37) + a(28) + a(19)

     For j91 = m1 To m2                                          'a(91)
     If b1(a1(j91)) = 0 Then GoTo 910
     If b(a1(j91)) = 0 Then b(a1(j91)) = a1(j91): c(91) = a1(j91) Else GoTo 910
     a(91) = a1(j91)

     a(41) = s1 / 2 - a(91): If b(a(41)) = 0 Then b(a(41)) = a(41): c(41) = a(41) Else GoTo 410

     a(10) = s10 - s2 - a(91)
     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(5) = s1 / 2 - a(10): If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 50
     
     For j92 = m1 To m2                                          'a(92)
     If b1(a1(j92)) = 0 Then GoTo 920
     If b(a1(j92)) = 0 Then b(a1(j92)) = a1(j92): c(92) = a1(j92) Else GoTo 920
     a(92) = a1(j92)

     a(42) = s1 / 2 - a(92): If b(a(42)) = 0 Then b(a(42)) = a(42): c(42) = a(42) Else GoTo 420

     For j93 = m1 To m2                                          'a(93)
     If b1(a1(j93)) = 0 Then GoTo 930
     If b(a1(j93)) = 0 Then b(a1(j93)) = a1(j93): c(93) = a1(j93) Else GoTo 930
     a(93) = a1(j93)

     a(43) = s1 / 2 - a(93): If b(a(43)) = 0 Then b(a(43)) = a(43): c(43) = a(43) Else GoTo 430

     a(94) = s1 - a(93) - a(92) - a(91)
     If a(94) < a1(m1) Or a(94) > a1(m2) Then GoTo 940
     If b1(a(94)) = 0 Then GoTo 940
     If b(a(94)) = 0 Then b(a(94)) = a(94): c(94) = a(94) Else GoTo 940

     a(44) = s1 / 2 - a(94): If b(a(44)) = 0 Then b(a(44)) = a(44): c(44) = a(44) Else GoTo 440

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

     a(15) = s1 / 2 - a(20): If b(a(15)) = 0 Then b(a(15)) = a(15): c(15) = a(15) Else GoTo 155

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

     a(25) = s1 / 2 - a(30): If b(a(25)) = 0 Then b(a(25)) = a(25): c(25) = a(25) Else GoTo 250

     a(40) = s1 - a(30) - a(20) - a(10)
     If a(40) < a1(m1) Or a(40) > a1(m2) Then GoTo 400
     If b1(a(40)) = 0 Then GoTo 400
     If b(a(40)) = 0 Then b(a(40)) = a(40): c(40) = a(40) Else GoTo 400

     a(35) = s1 / 2 - a(40): If b(a(35)) = 0 Then b(a(35)) = a(35): c(35) = a(35) Else GoTo 350

     Return

     b(c(35)) = 0: c(35) = 0
350  b(c(40)) = 0: c(40) = 0
400  b(c(25)) = 0: c(25) = 0
250  b(c(30)) = 0: c(30) = 0
300  Next j30

     b(c(15)) = 0: c(15) = 0
155  b(c(20)) = 0: c(20) = 0
200  Next j20

     b(c(44)) = 0: c(44) = 0
440  b(c(94)) = 0: c(94) = 0
940  b(c(43)) = 0: c(43) = 0
430  b(c(93)) = 0: c(93) = 0
930  Next j93

     b(c(42)) = 0: c(42) = 0
420  b(c(92)) = 0: c(92) = 0
920  Next j92

     b(c(5)) = 0: c(5) = 0
50   b(c(10)) = 0: c(10) = 0
100  b(c(41)) = 0: c(41) = 0
410  b(c(91)) = 0: c(91) = 0
910  Next j91

    fl2 = 0                 'No solution found
    Return

'    Print results (lines)

1640 Cells(n9, 100).Select
     For i1 = 1 To 100
         Cells(n9, i1).Value = a(i1)
     Next i1
    
     Return

'    Print results (squares)

1650 n2 = n2 + 1
     If n2 = 3 Then
         n2 = 1: k1 = k1 + 11: k2 = 1
     Else
         If n9 > 1 Then k2 = k2 + 11
     End If

     Cells(k1, k2 + 1).Select
     Cells(k1, k2 + 1).Font.Color = -4165632
     Cells(k1, k2 + 1).Value = "MC = " + CStr(MC10)
    
     i3 = 0
     For i1 = 1 To 10
         For i2 = 1 To 10
             i3 = i3 + 1
             Cells(k1 + i1, k2 + i2).Value = a(i3)
         Next i2
     Next i1

     Return
     
'    Read Prime Numbers From Sheet Sht1
     
2010 s1 = 2 * Sheets(Sht1).Cells(Rcrd1, 1).Value  'PM4
     s10 = 10 * s1 / 4                            'PM10
     nVar = Sheets(Sht1).Cells(Rcrd1, 5).Value
     nPM4 = Sheets(Sht1).Cells(Rcrd1, 6).Value
     
     If s10 <> MC10 Then                          'Check Sources
        t10 = "Record :" + CStr(Rcrd1) + Chr(13)
        t10 = t10 + "Line :" + CStr(j100)
        y = MsgBox(t10, vbCritical, "Conflict in Data")
        End
     End If
   
     m1 = 1: m2 = nVar
    
     For i1 = m1 To m2
        a1(i1) = Sheets(Sht1).Cells(Rcrd1, i1 + 6).Value
     Next i1
     a20 = a1(m2)                                 'Maximum
    
     Erase b1
     For i1 = m1 To m2
         b1(a1(i1)) = a1(i1)
     Next i1
     Return

End Sub

Vorige Pagina About the Author