Vorige Pagina About the Author

' Generates Inlaid Magic Squares of order 12
' Order 4 Sub Squares with Different Magic Sums

' Tested with Office 2007 under Windows 7

Sub Priem12b()

Dim a1(250), a(16), a12(144), b1(15355), b(15355), c(16)

y = MsgBox("Locked", vbCritical, "Routine Priem12b")
End
    
    n1 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
    
    Sheets("Klad1").Select
    
    t1 = Timer

For j200 = 94 To 94
s12 = Sheets("Input9").Cells(j200, 20).Value

Erase a12

For j101 = 11 To 19
j100 = Sheets("Input9").Cells(j200, j101).Value

'   Define variables

    Pr3 = Sheets("Pairs7").Cells(j100, 1).Value      'Pair Sum
    s1 = 2 * Pr3
    nVar1 = Sheets("Pairs7").Cells(j100, 9).Value
   
    For i1 = 1 To nVar1
        a1(i1) = Sheets("Pairs7").Cells(j100, 9 + i1).Value
    Next i1
    m1 = 1: m2 = nVar1
    If a1(1) = 1 Then m1 = 2: m2 = m2 - 1
    
    Erase b1
    For i1 = m1 To m2
        b1(a1(i1)) = a1(i1)
    Next i1

'   Remove Used Primes

    For i1 = 1 To 144
        b1(a12(i1)) = 0
    Next i1

'   Generate Squares

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)

a(6) = 0.5 * s1 - a(16):
If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 60
If b1(a(6)) = 0 Then GoTo 60
If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 60

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)

a(5) = 0.5 * s1 - a(15):
If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 50
If b1(a(5)) = 0 Then GoTo 50
If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 50

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

a(8) = 0.5 * s1 - a(14):
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(7) = 0.5 * s1 - a(13):
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

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 75
If b1(a(11)) = 0 Then GoTo 75

a(10) = a(12) - a(14) + a(16)
If a(10) < a1(m1) Or a(10) > a1(m2) Then GoTo 75
If b1(a(10)) = 0 Then GoTo 75

a(9) = -a(12) + a(14) + a(15)
If a(9) < a1(m1) Or a(9) > a1(m2) Then GoTo 75
If b1(a(9)) = 0 Then GoTo 75

a(4) = 0.5 * s1 - a(10)
If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 75:
If b1(a(4)) = 0 Then GoTo 75

a(3) = 0.5 * s1 - a(9)
If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 75:
If b1(a(3)) = 0 Then GoTo 75

a(2) = 0.5 * s1 - a(12)
If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 75:
If b1(a(2)) = 0 Then GoTo 75

a(1) = 0.5 * s1 - a(11)
If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 75:
If b1(a(1)) = 0 Then GoTo 75

'                         Exclude solutions with identical numbers

                          GoSub 800: If fl1 = 0 Then GoTo 75
                          n10 = n10 + 1: GoSub 750            'Assign to a12()
                          
                          Erase b, c: GoTo 5                  'Assign only first square

    
75  b(c(12)) = 0: c(12) = 0
120 Next j12

    b(c(7)) = 0: c(7) = 0
70  b(c(8)) = 0: c(8) = 0
80  b(c(13)) = 0: c(13) = 0
130 b(c(14)) = 0: c(14) = 0
140 Next j14
    
    b(c(5)) = 0: c(5) = 0
50  b(c(15)) = 0: c(15) = 0
150 Next j15

    b(c(6)) = 0: c(6) = 0
60  b(c(16)) = 0: c(16) = 0
160 Next j16

'   Not found
    Erase b, c: n10 = 0: GoTo 2000

5
    If n10 = 9 Then
        GoSub 850:
        If fl1 = 1 Then
           n9 = n9 + 1: GoSub 660   'Print results (squares)
           n10 = 0: GoTo 2000
        End If
    End If

      Next j101
      
2000  Next j200

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

End

'   Assign to a12()

750 Select Case n10

        Case 1
        a12(1) = a(1):   a12(2) = a(2):   a12(3) = a(3):   a12(4) = a(4):
        a12(13) = a(5):  a12(14) = a(6):  a12(15) = a(7):  a12(16) = a(8):
        a12(25) = a(9):  a12(26) = a(10): a12(27) = a(11): a12(28) = a(12):
        a12(37) = a(13): a12(38) = a(14): a12(39) = a(15): a12(40) = a(16):
        
        Case 2
        a12(5) = a(1):   a12(6) = a(2):   a12(7) = a(3):   a12(8) = a(4):
        a12(17) = a(5):  a12(18) = a(6):  a12(19) = a(7):  a12(20) = a(8):
        a12(29) = a(9):  a12(30) = a(10): a12(31) = a(11): a12(32) = a(12):
        a12(41) = a(13): a12(42) = a(14): a12(43) = a(15): a12(44) = a(16):
        
        Case 3
        a12(9) = a(1):   a12(10) = a(2):  a12(11) = a(3):  a12(12) = a(4):
        a12(21) = a(5):  a12(22) = a(6):  a12(23) = a(7):  a12(24) = a(8):
        a12(33) = a(9):  a12(34) = a(10): a12(35) = a(11): a12(36) = a(12):
        a12(45) = a(13): a12(46) = a(14): a12(47) = a(15): a12(48) = a(16):
        
        Case 4
        a12(49) = a(1):  a12(50) = a(2):  a12(51) = a(3):  a12(52) = a(4):
        a12(61) = a(5):  a12(62) = a(6):  a12(63) = a(7):  a12(64) = a(8):
        a12(73) = a(9):  a12(74) = a(10): a12(75) = a(11): a12(76) = a(12):
        a12(85) = a(13): a12(86) = a(14): a12(87) = a(15): a12(88) = a(16):
        
        Case 5
        a12(53) = a(1):  a12(54) = a(2):  a12(55) = a(3):  a12(56) = a(4):
        a12(65) = a(5):  a12(66) = a(6):  a12(67) = a(7):  a12(68) = a(8):
        a12(77) = a(9):  a12(78) = a(10): a12(79) = a(11): a12(80) = a(12):
        a12(89) = a(13): a12(90) = a(14): a12(91) = a(15): a12(92) = a(16):
        
        Case 6
        a12(57) = a(1):  a12(58) = a(2):  a12(59) = a(3):  a12(60) = a(4):
        a12(69) = a(5):  a12(70) = a(6):  a12(71) = a(7):  a12(72) = a(8):
        a12(81) = a(9):  a12(82) = a(10): a12(83) = a(11): a12(84) = a(12):
        a12(93) = a(13): a12(94) = a(14): a12(95) = a(15): a12(96) = a(16):
        
        Case 7
        a12(97) = a(1):   a12(98) = a(2):   a12(99) = a(3):   a12(100) = a(4):
        a12(109) = a(5):  a12(110) = a(6):  a12(111) = a(7):  a12(112) = a(8):
        a12(121) = a(9):  a12(122) = a(10): a12(123) = a(11): a12(124) = a(12):
        a12(133) = a(13): a12(134) = a(14): a12(135) = a(15): a12(136) = a(16):
        
        Case 8
        a12(101) = a(1):  a12(102) = a(2):  a12(103) = a(3):  a12(104) = a(4):
        a12(113) = a(5):  a12(114) = a(6):  a12(115) = a(7):  a12(116) = a(8):
        a12(125) = a(9):  a12(126) = a(10): a12(127) = a(11): a12(128) = a(12):
        a12(137) = a(13): a12(138) = a(14): a12(139) = a(15): a12(140) = a(16):
       
        Case 9
        a12(105) = a(1):  a12(106) = a(2):  a12(107) = a(3):  a12(108) = a(4):
        a12(117) = a(5):  a12(118) = a(6):  a12(119) = a(7):  a12(120) = a(8):
        a12(129) = a(9):  a12(130) = a(10): a12(131) = a(11): a12(132) = a(12):
        a12(141) = a(13): a12(142) = a(14): a12(143) = a(15): a12(144) = a(16):

    End Select
    
    Return

'   Print results: squares a12()

660 n1 = n1 + 1
    If n1 = 2 Then
        n1 = 1: k1 = k1 + 13: k2 = 1
    Else
        If n9 > 1 Then k2 = k2 + 13
    End If
    
    Cells(k1, k2 + 1).Select
    Cells(k1, k2 + 1).Font.Color = -4165632
    Cells(k1, k2 + 1).Value = s12
    
    i3 = 0
    For i1 = 1 To 12
        For i2 = 1 To 12
            i3 = i3 + 1
            Cells(k1 + i1, k2 + i2).Value = a12(i3)
        Next i2
    Next i1
    Return

'   Exclude solutions with identical numbers a()

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

'   Exclude solutions with identical numbers a12()

850 fl1 = 1
    For j1 = 1 To 144
       a2 = a12(j1): If a2 = 0 Then GoTo 860
       For j2 = (1 + j1) To 144
           If a2 = a12(j2) Then fl1 = 0: Return
       Next j2
860 Next j1
    Return

End Sub

Vorige Pagina About the Author