Vorige Pagina About the Author

' Generates Inlaid Magic Squares of order 18
' Based on Order 6 Magic Sub Squares with Different Magic Sums

' Tested with Office 2007 under Windows 7

Sub Priem18k2()

Dim a1(750), a(36), a18(324), b1(45355), b(45355), c(36)

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

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

Erase a18

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
    s6 = 3 * 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 324
        b1(a18(i1)) = 0
    Next i1

'   Generate Squares

For j29 = m1 To m2                                      'a(29) Center Square
If b1(a1(j29)) = 0 Then GoTo 290
If b(a1(j29)) = 0 Then b(a1(j29)) = a1(j29): c(29) = a1(j29) Else GoTo 290
a(29) = a1(j29)

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

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

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

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

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

a(17) = 0.5 * s1 - a(27):
If a(17) < a1(m1) Or a(17) > a1(m2) Then GoTo 170
If b1(a(17)) = 0 Then GoTo 170
If b(a(17)) = 0 Then b(a(17)) = a(17): c(17) = a(17) Else GoTo 170

a(16) = 0.5 * s1 - 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

For j23 = m1 To m2                                          'a(23) Center Square
If b1(a1(j23)) = 0 Then GoTo 230
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(28) - a(29)
If a(22) < a1(m1) Or a(22) > a1(m2) Then GoTo 220
If b1(a(22)) = 0 Then GoTo 220
If b(a(22)) = 0 Then b(a(22)) = a(22): c(22) = a(22) Else GoTo 220

a(21) = a(23) - a(27) + a(29)
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(20) = -a(23) + a(27) + a(28)
If a(20) < a1(m1) Or a(20) > a1(m2) Then GoTo 200
If b1(a(20)) = 0 Then GoTo 200
If b(a(20)) = 0 Then b(a(20)) = a(20): c(20) = a(20) Else GoTo 200

a(11) = 0.5 * s1 - a(21)
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(10) = 0.5 * s1 - a(20)
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(9) = 0.5 * s1 - a(23)
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(8) = 0.5 * s1 - a(22)
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
                          
For j36 = m1 To m2                                                'a(36)    Border
If b1(a1(j36)) = 0 Then GoTo 2360
If b(a1(j36)) = 0 Then b(a1(j36)) = a1(j36): c(36) = a1(j36) Else GoTo 2360
a(36) = a1(j36)

a(1) = s6 / 3 - a(36)
If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 2010
If b1(a(1)) = 0 Then GoTo 2010
If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 2010

For j35 = m1 To m2                                                'a(35)
If b1(a1(j35)) = 0 Then GoTo 2350
If b(a1(j35)) = 0 Then b(a1(j35)) = a1(j35): c(35) = a1(j35) Else GoTo 2350
a(35) = a1(j35)

a(5) = s6 / 3 - a(35)
If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 2050
If b1(a(5)) = 0 Then GoTo 2050
If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 2050

a(30) = 2 * s6 / 3 - a(29) - a(35) - a(36)
If a(30) < a1(m1) Or a(30) > a1(m2) Then GoTo 2300
If b1(a(30)) = 0 Then GoTo 2300
If b(a(30)) = 0 Then b(a(30)) = a(30): c(30) = a(30) Else GoTo 2300

a(25) = s6 / 3 - a(30)
If a(25) < a1(m1) Or a(25) > a1(m2) Then GoTo 2250
If b1(a(25)) = 0 Then GoTo 2250
If b(a(25)) = 0 Then b(a(25)) = a(25): c(25) = a(25) Else GoTo 2250

For j34 = m1 To m2                                                'a(34)
If b1(a1(j34)) = 0 Then GoTo 2340
If b(a1(j34)) = 0 Then b(a1(j34)) = a1(j34): c(34) = a1(j34) Else GoTo 2340
a(34) = a1(j34)

a(4) = s6 / 3 - a(34)
If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 2040
If b1(a(4)) = 0 Then GoTo 2040
If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 2040

a(33) = 2 * s6 / 3 - a(27) - a(28) - a(34)
If a(33) < a1(m1) Or a(33) > a1(m2) Then GoTo 2330
If b1(a(33)) = 0 Then GoTo 2330
If b(a(33)) = 0 Then b(a(33)) = a(33): c(33) = a(33) Else GoTo 2330

a(3) = s6 / 3 - a(33)
If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 2030
If b1(a(3)) = 0 Then GoTo 2030
If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 2030

For j32 = m1 To m2                                                'a(32)
If b1(a1(j32)) = 0 Then GoTo 2320
If b(a1(j32)) = 0 Then b(a1(j32)) = a1(j32): c(32) = a1(j32) Else GoTo 2320
a(32) = a1(j32)

a(2) = s6 / 3 - a(32)
If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 2020
If b1(a(2)) = 0 Then GoTo 2020
If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 2020

a(31) = s6 - a(32) - a(33) - a(34) - a(35) - a(36)
If a(31) < a1(m1) Or a(31) > a1(m2) Then GoTo 2310
If b1(a(31)) = 0 Then GoTo 2310
If b(a(31)) = 0 Then b(a(31)) = a(31): c(31) = a(31) Else GoTo 2310

a(6) = s6 / 3 - a(31)
If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 2060
If b1(a(6)) = 0 Then GoTo 2060
If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 2060

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

a(19) = s6 / 3 - a(24)
If a(19) < a1(m1) Or a(19) > a1(m2) Then GoTo 2190
If b1(a(19)) = 0 Then GoTo 2190
If b(a(19)) = 0 Then b(a(19)) = a(19): c(19) = a(19) Else GoTo 2190

a(13) = 2 * s6 / 3 - a(14) - a(19) - a(20)
If a(13) < a1(m1) Or a(13) > a1(m2) Then GoTo 2130
If b1(a(13)) = 0 Then GoTo 2130
If b(a(13)) = 0 Then b(a(13)) = a(13): c(13) = a(13) Else GoTo 2130

a(18) = s6 / 3 - a(13)
If a(18) < a1(m1) Or a(18) > a1(m2) Then GoTo 2180
If b1(a(18)) = 0 Then GoTo 2180
If b(a(18)) = 0 Then b(a(18)) = a(18): c(18) = a(18) Else GoTo 2180

a(12) = s6 - a(6) - a(18) - a(24) - a(30) - a(36)
If a(12) < a1(m1) Or a(12) > a1(m2) Then GoTo 2120
If b1(a(12)) = 0 Then GoTo 2120
If b(a(12)) = 0 Then b(a(12)) = a(12): c(12) = a(12) Else GoTo 2120

a(7) = s6 / 3 - a(12)
If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 2070
If b1(a(7)) = 0 Then GoTo 2070
If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 2070


'                         Exclude solutions with identical numbers

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

75
     
     b(c(7)) = 0: c(7) = 0
2070 b(c(12)) = 0: c(12) = 0
2120 b(c(18)) = 0: c(18) = 0
2180 b(c(13)) = 0: c(13) = 0
2130 b(c(19)) = 0: c(19) = 0
2190 b(c(24)) = 0: c(24) = 0
2240 Next j24
     
     b(c(6)) = 0: c(6) = 0
2060 b(c(31)) = 0: c(31) = 0
2310 b(c(2)) = 0: c(2) = 0
2020 b(c(32)) = 0: c(32) = 0
2320 Next j32

     b(c(3)) = 0: c(3) = 0
2030 b(c(33)) = 0: c(33) = 0
2330 b(c(4)) = 0: c(4) = 0
2040 b(c(34)) = 0: c(34) = 0
2340 Next j34

     b(c(25)) = 0: c(25) = 0
2250 b(c(30)) = 0: c(30) = 0
2300 b(c(5)) = 0: c(5) = 0
2050 b(c(35)) = 0: c(35) = 0
2350 Next j35
    
     b(c(1)) = 0: c(1) = 0
2010 b(c(36)) = 0: c(36) = 0
2360 Next j36

    b(c(8)) = 0: c(8) = 0
80  b(c(9)) = 0: c(9) = 0
90  b(c(10)) = 0: c(10) = 0
100 b(c(11)) = 0: c(11) = 0
110 b(c(20)) = 0: c(20) = 0
200 b(c(21)) = 0: c(21) = 0
210 b(c(22)) = 0: c(22) = 0
220 b(c(23)) = 0: c(23) = 0
230 Next j23

    b(c(16)) = 0: c(16) = 0
160  b(c(17)) = 0: c(17) = 0
170  b(c(26)) = 0: c(26) = 0
260 b(c(27)) = 0: c(27) = 0
270 Next j27
    
    b(c(14)) = 0: c(14) = 0
140  b(c(28)) = 0: c(28) = 0
280 Next j28

    b(c(15)) = 0: c(15) = 0
150  b(c(29)) = 0: c(29) = 0
290 Next j29


'   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  n10 = 0
      Next j200

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

End

'   Assign to a18()

750 Select Case n10

    Case 1
    a18(1) = a(1):  a18(2) = a(2):  a18(3) = a(3):  a18(4) = a(4):  a18(5) = a(5):  a18(6) = a(6):
    a18(19) = a(7): a18(20) = a(8): a18(21) = a(9): a18(22) = a(10): a18(23) = a(11): a18(24) = a(12):
    a18(37) = a(13): a18(38) = a(14): a18(39) = a(15): a18(40) = a(16): a18(41) = a(17): a18(42) = a(18):
    a18(55) = a(19): a18(56) = a(20): a18(57) = a(21): a18(58) = a(22): a18(59) = a(23): a18(60) = a(24):
    a18(73) = a(25): a18(74) = a(26): a18(75) = a(27): a18(76) = a(28): a18(77) = a(29): a18(78) = a(30):
    a18(91) = a(31): a18(92) = a(32): a18(93) = a(33): a18(94) = a(34): a18(95) = a(35): a18(96) = a(36):
            
    Case 2
    a18(7) = a(1):  a18(8) = a(2):  a18(9) = a(3):  a18(10) = a(4): a18(11) = a(5): a18(12) = a(6):
    a18(25) = a(7): a18(26) = a(8): a18(27) = a(9): a18(28) = a(10): a18(29) = a(11): a18(30) = a(12):
    a18(43) = a(13): a18(44) = a(14): a18(45) = a(15): a18(46) = a(16): a18(47) = a(17): a18(48) = a(18):
    a18(61) = a(19): a18(62) = a(20): a18(63) = a(21): a18(64) = a(22): a18(65) = a(23): a18(66) = a(24):
    a18(79) = a(25): a18(80) = a(26): a18(81) = a(27): a18(82) = a(28): a18(83) = a(29): a18(84) = a(30):
    a18(97) = a(31): a18(98) = a(32): a18(99) = a(33): a18(100) = a(34): a18(101) = a(35): a18(102) = a(36):
            
    Case 3
    a18(13) = a(1): a18(14) = a(2): a18(15) = a(3): a18(16) = a(4): a18(17) = a(5): a18(18) = a(6):
    a18(31) = a(7): a18(32) = a(8): a18(33) = a(9): a18(34) = a(10): a18(35) = a(11): a18(36) = a(12):
    a18(49) = a(13): a18(50) = a(14): a18(51) = a(15): a18(52) = a(16): a18(53) = a(17): a18(54) = a(18):
    a18(67) = a(19): a18(68) = a(20): a18(69) = a(21): a18(70) = a(22): a18(71) = a(23): a18(72) = a(24):
    a18(85) = a(25): a18(86) = a(26): a18(87) = a(27): a18(88) = a(28): a18(89) = a(29): a18(90) = a(30):
    a18(103) = a(31): a18(104) = a(32): a18(105) = a(33): a18(106) = a(34): a18(107) = a(35): a18(108) = a(36):
            
    Case 4
    a18(109) = a(1): a18(110) = a(2): a18(111) = a(3): a18(112) = a(4): a18(113) = a(5): a18(114) = a(6):
    a18(127) = a(7): a18(128) = a(8): a18(129) = a(9): a18(130) = a(10): a18(131) = a(11): a18(132) = a(12):
    a18(145) = a(13): a18(146) = a(14): a18(147) = a(15): a18(148) = a(16): a18(149) = a(17): a18(150) = a(18):
    a18(163) = a(19): a18(164) = a(20): a18(165) = a(21): a18(166) = a(22): a18(167) = a(23): a18(168) = a(24):
    a18(181) = a(25): a18(182) = a(26): a18(183) = a(27): a18(184) = a(28): a18(185) = a(29): a18(186) = a(30):
    a18(199) = a(31): a18(200) = a(32): a18(201) = a(33): a18(202) = a(34): a18(203) = a(35): a18(204) = a(36):
            
    Case 5
    a18(115) = a(1): a18(116) = a(2): a18(117) = a(3): a18(118) = a(4): a18(119) = a(5): a18(120) = a(6):
    a18(133) = a(7): a18(134) = a(8): a18(135) = a(9): a18(136) = a(10): a18(137) = a(11): a18(138) = a(12):
    a18(151) = a(13): a18(152) = a(14): a18(153) = a(15): a18(154) = a(16): a18(155) = a(17): a18(156) = a(18):
    a18(169) = a(19): a18(170) = a(20): a18(171) = a(21): a18(172) = a(22): a18(173) = a(23): a18(174) = a(24):
    a18(187) = a(25): a18(188) = a(26): a18(189) = a(27): a18(190) = a(28): a18(191) = a(29): a18(192) = a(30):
    a18(205) = a(31): a18(206) = a(32): a18(207) = a(33): a18(208) = a(34): a18(209) = a(35): a18(210) = a(36):
            
    Case 6
    a18(121) = a(1): a18(122) = a(2): a18(123) = a(3): a18(124) = a(4): a18(125) = a(5): a18(126) = a(6):
    a18(139) = a(7): a18(140) = a(8): a18(141) = a(9): a18(142) = a(10): a18(143) = a(11): a18(144) = a(12):
    a18(157) = a(13): a18(158) = a(14): a18(159) = a(15): a18(160) = a(16): a18(161) = a(17): a18(162) = a(18):
    a18(175) = a(19): a18(176) = a(20): a18(177) = a(21): a18(178) = a(22): a18(179) = a(23): a18(180) = a(24):
    a18(193) = a(25): a18(194) = a(26): a18(195) = a(27): a18(196) = a(28): a18(197) = a(29): a18(198) = a(30):
    a18(211) = a(31): a18(212) = a(32): a18(213) = a(33): a18(214) = a(34): a18(215) = a(35): a18(216) = a(36):
            
    Case 7
    a18(217) = a(1): a18(218) = a(2): a18(219) = a(3): a18(220) = a(4): a18(221) = a(5): a18(222) = a(6):
    a18(235) = a(7): a18(236) = a(8): a18(237) = a(9): a18(238) = a(10): a18(239) = a(11): a18(240) = a(12):
    a18(253) = a(13): a18(254) = a(14): a18(255) = a(15): a18(256) = a(16): a18(257) = a(17): a18(258) = a(18):
    a18(271) = a(19): a18(272) = a(20): a18(273) = a(21): a18(274) = a(22): a18(275) = a(23): a18(276) = a(24):
    a18(289) = a(25): a18(290) = a(26): a18(291) = a(27): a18(292) = a(28): a18(293) = a(29): a18(294) = a(30):
    a18(307) = a(31): a18(308) = a(32): a18(309) = a(33): a18(310) = a(34): a18(311) = a(35): a18(312) = a(36):
            
    Case 8
    a18(223) = a(1): a18(224) = a(2): a18(225) = a(3): a18(226) = a(4): a18(227) = a(5): a18(228) = a(6):
    a18(241) = a(7): a18(242) = a(8): a18(243) = a(9): a18(244) = a(10): a18(245) = a(11): a18(246) = a(12):
    a18(259) = a(13): a18(260) = a(14): a18(261) = a(15): a18(262) = a(16): a18(263) = a(17): a18(264) = a(18):
    a18(277) = a(19): a18(278) = a(20): a18(279) = a(21): a18(280) = a(22): a18(281) = a(23): a18(282) = a(24):
    a18(295) = a(25): a18(296) = a(26): a18(297) = a(27): a18(298) = a(28): a18(299) = a(29): a18(300) = a(30):
    a18(313) = a(31): a18(314) = a(32): a18(315) = a(33): a18(316) = a(34): a18(317) = a(35): a18(318) = a(36):
           
    Case 9
    a18(229) = a(1): a18(230) = a(2): a18(231) = a(3): a18(232) = a(4): a18(233) = a(5): a18(234) = a(6):
    a18(247) = a(7): a18(248) = a(8): a18(249) = a(9): a18(250) = a(10): a18(251) = a(11): a18(252) = a(12):
    a18(265) = a(13): a18(266) = a(14): a18(267) = a(15): a18(268) = a(16): a18(269) = a(17): a18(270) = a(18):
    a18(283) = a(19): a18(284) = a(20): a18(285) = a(21): a18(286) = a(22): a18(287) = a(23): a18(288) = a(24):
    a18(301) = a(25): a18(302) = a(26): a18(303) = a(27): a18(304) = a(28): a18(305) = a(29): a18(306) = a(30):
    a18(319) = a(31): a18(320) = a(32): a18(321) = a(33): a18(322) = a(34): a18(323) = a(35): a18(324) = a(36):

    End Select
    
    Return

'   Print results: squares a18()

660 n1 = n1 + 1
    If n1 = 2 Then
        n1 = 1: k1 = k1 + 19: k2 = 1
    Else
        If n9 > 1 Then k2 = k2 + 19
    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 18
        For i2 = 1 To 18
            i3 = i3 + 1
            Cells(k1 + i1, k2 + i2).Value = a18(i3)
        Next i2
    Next i1
   
    Return

'   Exclude solutions with identical numbers a()

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

'   Exclude solutions with identical numbers a18()

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

End Sub

Vorige Pagina About the Author