Vorige Pagina About the Author

' Generates Inlaid Magic Squares of order 16
' Based on Order 4 Magic Sub Squares with Different Magic Sums

' Tested with Office 2007 under Windows 7

Sub Priem16k()

Dim a1(550), a(16), a16(256), b1(25355), b(25355), c(16)

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

For j200 = 2 To 699
s16 = Sheets("Input12").Cells(j200, 34).Value

Erase a16

For j101 = 18 To 33
j100 = Sheets("Input12").Cells(j200, j101).Value

'   Define variables

    Cntr3 = Sheets("Pairs7").Cells(j100, 6).Value      'Mid
    s1 = 4 * Cntr3
    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 256
        b1(a16(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)

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

                          GoSub 800: If fl1 = 0 Then GoTo 70
                          n10 = n10 + 1: GoSub 750            'Assign to a16()
                          
                          Erase b, c: GoTo 5                  'Assign only first square
    
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

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

5
    If n10 = 16 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 Priem16k")

End

'   Assign to a16()

750 Select Case n10

Case 1
a16(1) = a(1):   a16(2) = a(2):   a16(3) = a(3):   a16(4) = a(4):
a16(17) = a(5):  a16(18) = a(6):  a16(19) = a(7):  a16(20) = a(8):
a16(33) = a(9):  a16(34) = a(10): a16(35) = a(11): a16(36) = a(12):
a16(49) = a(13): a16(50) = a(14): a16(51) = a(15): a16(52) = a(16):
            
Case 2
a16(5) = a(1):   a16(6) = a(2):   a16(7) = a(3):   a16(8) = a(4):
a16(21) = a(5):  a16(22) = a(6):  a16(23) = a(7):  a16(24) = a(8):
a16(37) = a(9):  a16(38) = a(10): a16(39) = a(11): a16(40) = a(12):
a16(53) = a(13): a16(54) = a(14): a16(55) = a(15): a16(56) = a(16):
            
Case 3
a16(9) = a(1):   a16(10) = a(2):  a16(11) = a(3):  a16(12) = a(4):
a16(25) = a(5):  a16(26) = a(6):  a16(27) = a(7):  a16(28) = a(8):
a16(41) = a(9):  a16(42) = a(10): a16(43) = a(11): a16(44) = a(12):
a16(57) = a(13): a16(58) = a(14): a16(59) = a(15): a16(60) = a(16):
            
Case 4
a16(13) = a(1): a16(14) = a(2): a16(15) = a(3): a16(16) = a(4):
a16(29) = a(5): a16(30) = a(6): a16(31) = a(7): a16(32) = a(8):
a16(45) = a(9): a16(46) = a(10): a16(47) = a(11): a16(48) = a(12):
a16(61) = a(13): a16(62) = a(14): a16(63) = a(15): a16(64) = a(16):
            
Case 5
a16(65) = a(1):   a16(66) = a(2):   a16(67) = a(3):   a16(68) = a(4):
a16(81) = a(5):   a16(82) = a(6):   a16(83) = a(7):   a16(84) = a(8):
a16(97) = a(9):   a16(98) = a(10):  a16(99) = a(11):  a16(100) = a(12):
a16(113) = a(13): a16(114) = a(14): a16(115) = a(15): a16(116) = a(16):
            
Case 6
a16(69) = a(1):   a16(70) = a(2):   a16(71) = a(3):   a16(72) = a(4):
a16(85) = a(5):   a16(86) = a(6):   a16(87) = a(7):   a16(88) = a(8):
a16(101) = a(9):  a16(102) = a(10): a16(103) = a(11): a16(104) = a(12):
a16(117) = a(13): a16(118) = a(14): a16(119) = a(15): a16(120) = a(16):
            
Case 7
a16(73) = a(1):   a16(74) = a(2):   a16(75) = a(3):   a16(76) = a(4):
a16(89) = a(5):   a16(90) = a(6):   a16(91) = a(7):   a16(92) = a(8):
a16(105) = a(9):  a16(106) = a(10): a16(107) = a(11): a16(108) = a(12):
a16(121) = a(13): a16(122) = a(14): a16(123) = a(15): a16(124) = a(16):
            
Case 8
a16(77) = a(1):   a16(78) = a(2):   a16(79) = a(3):    a16(80) = a(4):
a16(93) = a(5):   a16(94) = a(6):   a16(95) = a(7):    a16(96) = a(8):
a16(109) = a(9):  a16(110) = a(10):  a16(111) = a(11): a16(112) = a(12):
a16(125) = a(13): a16(126) = a(14):  a16(127) = a(15): a16(128) = a(16):
            
Case 9
a16(129) = a(1):  a16(130) = a(2):  a16(131) = a(3):  a16(132) = a(4):
a16(145) = a(5):  a16(146) = a(6):  a16(147) = a(7):  a16(148) = a(8):
a16(161) = a(9):  a16(162) = a(10): a16(163) = a(11): a16(164) = a(12):
a16(177) = a(13): a16(178) = a(14): a16(179) = a(15): a16(180) = a(16):
    
Case 10
a16(133) = a(1):  a16(134) = a(2):   a16(135) = a(3):  a16(136) = a(4):
a16(149) = a(5):  a16(150) = a(6):   a16(151) = a(7):  a16(152) = a(8):
a16(165) = a(9):  a16(166) = a(10):  a16(167) = a(11): a16(168) = a(12):
a16(181) = a(13):  a16(182) = a(14): a16(183) = a(15): a16(184) = a(16):
            
Case 11
a16(137) = a(1):  a16(138) = a(2):  a16(139) = a(3):  a16(140) = a(4):
a16(153) = a(5):  a16(154) = a(6):  a16(155) = a(7):  a16(156) = a(8):
a16(169) = a(9):  a16(170) = a(10): a16(171) = a(11): a16(172) = a(12):
a16(185) = a(13): a16(186) = a(14): a16(187) = a(15): a16(188) = a(16):
            
Case 12
a16(141) = a(1):  a16(142) = a(2):  a16(143) = a(3):  a16(144) = a(4):
a16(157) = a(5):  a16(158) = a(6):  a16(159) = a(7):  a16(160) = a(8):
a16(173) = a(9):  a16(174) = a(10): a16(175) = a(11): a16(176) = a(12):
a16(189) = a(13): a16(190) = a(14): a16(191) = a(15): a16(192) = a(16):
            
Case 13
a16(193) = a(1):  a16(194) = a(2):  a16(195) = a(3):  a16(196) = a(4):
a16(209) = a(5):  a16(210) = a(6):  a16(211) = a(7):  a16(212) = a(8):
a16(225) = a(9):  a16(226) = a(10): a16(227) = a(11): a16(228) = a(12):
a16(241) = a(13): a16(242) = a(14): a16(243) = a(15): a16(244) = a(16):
            
Case 14
a16(197) = a(1):  a16(198) = a(2):  a16(199) = a(3):  a16(200) = a(4):
a16(213) = a(5):  a16(214) = a(6):  a16(215) = a(7):  a16(216) = a(8):
a16(229) = a(9):  a16(230) = a(10): a16(231) = a(11): a16(232) = a(12):
a16(245) = a(13): a16(246) = a(14): a16(247) = a(15): a16(248) = a(16):
    
Case 15
a16(201) = a(1):  a16(202) = a(2):  a16(203) = a(3):  a16(204) = a(4):
a16(217) = a(5):  a16(218) = a(6):  a16(219) = a(7):  a16(220) = a(8):
a16(233) = a(9):  a16(234) = a(10): a16(235) = a(11): a16(236) = a(12):
a16(249) = a(13): a16(250) = a(14): a16(251) = a(15): a16(252) = a(16):
            
Case 16
a16(205) = a(1):  a16(206) = a(2):  a16(207) = a(3):  a16(208) = a(4):
a16(221) = a(5):  a16(222) = a(6):  a16(223) = a(7):  a16(224) = a(8):
a16(237) = a(9):  a16(238) = a(10): a16(239) = a(11): a16(240) = a(12):
a16(253) = a(13): a16(254) = a(14): a16(255) = a(15): a16(256) = a(16):

    End Select
    Return

'   Print results: squares a16()

660 n1 = n1 + 1
    If n1 = 2 Then
        n1 = 1: k1 = k1 + 17: k2 = 1
    Else
        If n9 > 1 Then k2 = k2 + 17
    End If
    
    Cells(k1, k2 + 1).Select
    Cells(k1, k2 + 1).Font.Color = -4165632
    Cells(k1, k2 + 1).Value = s16
    
    i3 = 0
    For i1 = 1 To 16
        For i2 = 1 To 16
            i3 = i3 + 1
            Cells(k1 + i1, k2 + i2).Value = a16(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 a16()

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

End Sub

Vorige Pagina About the Author