Vorige Pagina About the Author

' Constructs 10 x 10 Magic Squares with Non Overlapping Subsquares (Distinct Prime Numbers):
' - Reads Prime Number Eccentric 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 Priem10b()

    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 Priem10b")
End

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

'   Generate Squares

    Sheets("Klad1").Select
    
    t1 = Timer
    
For j100 = 90 To 113

'   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 Priem10b")

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(61) = a4(1):  a(62) = a4(2):  a(63) = a4(3):  a(64) = a4(4):
     a(71) = a4(5):  a(72) = a4(6):  a(73) = a4(7):  a(74) = a4(8):
     a(81) = a4(9):  a(82) = a4(10): a(83) = a4(11): a(84) = a4(12):
     a(91) = a4(13): a(92) = a4(14): a(93) = a4(15): a(94) = 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(7) = c4(1):   a(8) = c4(2):   a(9) = c4(3):   a(10) = c4(4):
     a(17) = c4(5):  a(18) = c4(6):  a(19) = c4(7):  a(20) = c4(8):
     a(27) = c4(9):  a(28) = c4(10): a(29) = c4(11): a(30) = c4(12):
     a(37) = c4(13): a(38) = c4(14): a(39) = c4(15): a(40) = c4(16):

     Return
     
'    Exclude solutions with identical numbers a()

800  fl1 = 1
     For j1 = 1 To n8
        a2 = a(j1)
        For j2 = (1 + j1) To n8
            If a2 = a(j2) Then fl1 = 0: Return
        Next j2
     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

    For j6 = m1 To m2                                          'a(6)
    If b1(a1(j6)) = 0 Then GoTo 60
    If b(a1(j6)) = 0 Then b(a1(j6)) = a1(j6): c(6) = a1(j6) Else GoTo 60
    a(6) = a1(j6)

    a(5) = s1 / 2 - a(6): If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 50
    
    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 155
    a(15) = a1(j15)

    a(16) = s1 / 2 - a(15): If b(a(16)) = 0 Then b(a(16)) = a(16): c(16) = a(16) Else GoTo 165
    
    For j51 = m1 To m2                                          'a(51)
    If b1(a1(j51)) = 0 Then GoTo 510
    If b(a1(j51)) = 0 Then b(a1(j51)) = a1(j51): c(51) = a1(j51) Else GoTo 510
    a(51) = a1(j51)
    
    a(41) = s1 / 2 - a(51): If b(a(41)) = 0 Then b(a(41)) = a(41): c(41) = a(41) Else GoTo 410
    
    For j42 = m1 To m2                                          'a(42)
    If b1(a1(j42)) = 0 Then GoTo 420
    If b(a1(j42)) = 0 Then b(a1(j42)) = a1(j42): c(42) = a1(j42) Else GoTo 420
    a(42) = a1(j42)
 
    a(52) = s1 / 2 - a(42): If b(a(52)) = 0 Then b(a(52)) = a(52): c(52) = a(52) Else GoTo 520
    
    For j36 = m1 To m2                                          'a(36)
    If b1(a1(j36)) = 0 Then GoTo 360
    If b(a1(j36)) = 0 Then b(a1(j36)) = a1(j36): c(36) = a1(j36) Else GoTo 360
    a(36) = a1(j36)

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

    a(25) = a(36) - a(15) + a(6)
    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(26) = s1 / 2 - a(25): If b(a(26)) = 0 Then b(a(26)) = a(26): c(26) = a(26) Else GoTo 260
    
    For j54 = m1 To m2                                          'a(54)
    If b1(a1(j54)) = 0 Then GoTo 540
    If b(a1(j54)) = 0 Then b(a1(j54)) = a1(j54): c(54) = a1(j54) Else GoTo 540
    a(54) = a1(j54)

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

    a(43) = a(54) + a(51) - a(42)
    If a(43) < a1(m1) Or a(43) > a1(m2) Then GoTo 430
    If b1(a(43)) = 0 Then GoTo 430
    If b(a(43)) = 0 Then b(a(43)) = a(43): c(43) = a(43) Else GoTo 430

    a(53) = s1 / 2 - a(43): If b(a(53)) = 0 Then b(a(53)) = a(53): c(53) = a(53) Else GoTo 530
 
    Return

    b(c(53)) = 0: c(53) = 0
530 b(c(43)) = 0: c(43) = 0
430 b(c(44)) = 0: c(44) = 0
440 b(c(54)) = 0: c(54) = 0
540 Next j54
    
    b(c(26)) = 0: c(26) = 0
260 b(c(25)) = 0: c(25) = 0
250 b(c(35)) = 0: c(35) = 0
350 b(c(36)) = 0: c(36) = 0
360 Next j36
    
   b(c(52)) = 0: c(52) = 0
520 b(c(42)) = 0: c(42) = 0
420 Next j42
    
    b(c(41)) = 0: c(41) = 0
410 b(c(51)) = 0: c(51) = 0
510 Next j51
    
    b(c(16)) = 0: c(16) = 0
165 b(c(15)) = 0: c(15) = 0
155 Next j15
    
    b(c(5)) = 0: c(5) = 0
50  b(c(6)) = 0: c(6) = 0
60 Next j6


    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

'    Check Type
     
     If a(1) + a(8) <> s1 / 2 Or a(2) + a(7) <> s1 / 2 Then
        t10 = "Line :" + CStr(j100) + Chr(13)
        t10 = t10 + "a(1) + a(8) = " + CStr(a(1) + a(8)) + Chr(13)
        t10 = t10 + "a(2) + a(7) = " + CStr(a(2) + a(7))
        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