' Generates Inlaid Magic Squares of order 18
' Based on Order 3 Magic Sub Squares with Different Magic Sums
' Tested with Office 2007 under Windows 7
Sub Priem18k1()
Dim a1(750), a(36), a18(324), b1(48355), b(48355), c(36)
y = MsgBox("Locked", vbCritical, "Routine Priem18k1")
End
n1 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
Sheets("Klad1").Select
t1 = Timer
For j200 = 2 To 723
s18 = Sheets("Input18").Cells(j200, 74).Value
Erase a18
'Define Center Elements
For i1 = 1 To 36
a(i1) = Sheets("Input18").Cells(j200, i1).Value
Next i1
a18(20) = a(1): a18(23) = a(2): a18(26) = a(3): a18(29) = a(4): a18(32) = a(5): a18(35) = a(6)
a18(74) = a(7): a18(77) = a(8): a18(80) = a(9): a18(83) = a(10): a18(86) = a(11): a18(89) = a(12)
a18(128) = a(13): a18(131) = a(14): a18(134) = a(15): a18(137) = a(16): a18(140) = a(17): a18(143) = a(18)
a18(182) = a(19): a18(185) = a(20): a18(188) = a(21): a18(191) = a(22): a18(194) = a(23): a18(197) = a(24)
a18(236) = a(25): a18(239) = a(26): a18(242) = a(27): a18(245) = a(28): a18(248) = a(29): a18(251) = a(30)
a18(290) = a(31): a18(293) = a(32): a18(296) = a(33): a18(299) = a(34): a18(302) = a(35): a18(305) = a(36)
Erase a
For j101 = 38 To 73
j100 = Sheets("Input18").Cells(j200, j101).Value
' Define variables
Cntr3 = Sheets("Pairs7").Cells(j100, 6).Value 'Mid
s1 = 3 * 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 324
b1(a18(i1)) = 0
Next i1
' Generate Squares
For j9 = m1 To m2 'a(9)
If b1(a1(j9)) = 0 Then GoTo 160
If b(a1(j9)) = 0 Then b(a1(j9)) = a1(j9): c(9) = a1(j9) Else GoTo 160
a(9) = a1(j9)
For j8 = m1 To m2 'a(8)
If b1(a1(j8)) = 0 Then GoTo 120
If b(a1(j8)) = 0 Then b(a1(j8)) = a1(j8): c(8) = a1(j8) Else GoTo 120
a(8) = a1(j8)
a(7) = s1 - a(8) - a(9):
If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 110:
If b1(a(7)) = 0 Then GoTo 110
a(6) = 4 * s1 / 3 - a(8) - 2 * a(9):
If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 110:
If b1(a(6)) = 0 Then GoTo 110
a(5) = s1 / 3:
If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 110:
''If b1(a(5)) = 0 Then GoTo 110
a(4) = -2 * s1 / 3 + a(8) + 2 * a(9):
If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 110:
If b1(a(4)) = 0 Then GoTo 110
a(3) = -s1 / 3 + a(8) + a(9):
If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 110:
If b1(a(3)) = 0 Then GoTo 110
a(2) = 2 * s1 / 3 - a(8):
If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 110:
If b1(a(2)) = 0 Then GoTo 110
a(1) = 2 * s1 / 3 - a(9):
If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 110:
If b1(a(1)) = 0 Then GoTo 110
' Exclude solutions with identical numbers
GoSub 800: If fl1 = 0 Then GoTo 110
n10 = n10 + 1: GoSub 750 'Assign to a18()
Erase b, c: GoTo 5 'Assign only first square
110 b(c(8)) = 0: c(8) = 0
120 Next j8
b(c(9)) = 0: c(9) = 0
160 Next j9
' Not found
Erase b, c: n10 = 0: GoTo 2000
5
If n10 = 36 Then
GoSub 850:
If fl1 = 1 Then
n9 = n9 + 1: GoSub 660 'Print results (squares)
If n9 = 8 Then End
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 Priem18k1")
End
' Assign to a18()
750 Select Case n10
Case 1
a18(1) = a(1): a18(2) = a(2): a18(3) = a(3):
a18(19) = a(4): a18(20) = a(5): a18(21) = a(6):
a18(37) = a(7): a18(38) = a(8): a18(39) = a(9):
Case 2
a18(4) = a(1): a18(5) = a(2): a18(6) = a(3):
a18(22) = a(4): a18(23) = a(5): a18(24) = a(6):
a18(40) = a(7): a18(41) = a(8): a18(42) = a(9):
Case 3
a18(7) = a(1): a18(8) = a(2): a18(9) = a(3):
a18(25) = a(4): a18(26) = a(5): a18(27) = a(6):
a18(43) = a(7): a18(44) = a(8): a18(45) = a(9):
Case 4
a18(10) = a(1): a18(11) = a(2): a18(12) = a(3):
a18(28) = a(4): a18(29) = a(5): a18(30) = a(6):
a18(46) = a(7): a18(47) = a(8): a18(48) = a(9):
Case 5
a18(13) = a(1): a18(14) = a(2): a18(15) = a(3):
a18(31) = a(4): a18(32) = a(5): a18(33) = a(6):
a18(49) = a(7): a18(50) = a(8): a18(51) = a(9):
Case 6
a18(16) = a(1): a18(17) = a(2): a18(18) = a(3):
a18(34) = a(4): a18(35) = a(5): a18(36) = a(6):
a18(52) = a(7): a18(53) = a(8): a18(54) = a(9):
Case 7
a18(55) = a(1): a18(56) = a(2): a18(57) = a(3):
a18(73) = a(4): a18(74) = a(5): a18(75) = a(6):
a18(91) = a(7): a18(92) = a(8): a18(93) = a(9):
Case 8
a18(58) = a(1): a18(59) = a(2): a18(60) = a(3):
a18(76) = a(4): a18(77) = a(5): a18(78) = a(6):
a18(94) = a(7): a18(95) = a(8): a18(96) = a(9):
Case 9
a18(61) = a(1): a18(62) = a(2): a18(63) = a(3):
a18(79) = a(4): a18(80) = a(5): a18(81) = a(6):
a18(97) = a(7): a18(98) = a(8): a18(99) = a(9):
Case 10
a18(64) = a(1): a18(65) = a(2): a18(66) = a(3):
a18(82) = a(4): a18(83) = a(5): a18(84) = a(6):
a18(100) = a(7): a18(101) = a(8): a18(102) = a(9):
Case 11
a18(67) = a(1): a18(68) = a(2): a18(69) = a(3):
a18(85) = a(4): a18(86) = a(5): a18(87) = a(6):
a18(103) = a(7): a18(104) = a(8): a18(105) = a(9):
Case 12
a18(70) = a(1): a18(71) = a(2): a18(72) = a(3):
a18(88) = a(4): a18(89) = a(5): a18(90) = a(6):
a18(106) = a(7): a18(107) = a(8): a18(108) = a(9):
Case 13
a18(109) = a(1): a18(110) = a(2): a18(111) = a(3):
a18(127) = a(4): a18(128) = a(5): a18(129) = a(6):
a18(145) = a(7): a18(146) = a(8): a18(147) = a(9):
Case 14
a18(112) = a(1): a18(113) = a(2): a18(114) = a(3):
a18(130) = a(4): a18(131) = a(5): a18(132) = a(6):
a18(148) = a(7): a18(149) = a(8): a18(150) = a(9):
Case 15
a18(115) = a(1): a18(116) = a(2): a18(117) = a(3):
a18(133) = a(4): a18(134) = a(5): a18(135) = a(6):
a18(151) = a(7): a18(152) = a(8): a18(153) = a(9):
Case 16
a18(118) = a(1): a18(119) = a(2): a18(120) = a(3):
a18(136) = a(4): a18(137) = a(5): a18(138) = a(6):
a18(154) = a(7): a18(155) = a(8): a18(156) = a(9):
Case 17
a18(121) = a(1): a18(122) = a(2): a18(123) = a(3):
a18(139) = a(4): a18(140) = a(5): a18(141) = a(6):
a18(157) = a(7): a18(158) = a(8): a18(159) = a(9):
Case 18
a18(124) = a(1): a18(125) = a(2): a18(126) = a(3):
a18(142) = a(4): a18(143) = a(5): a18(144) = a(6):
a18(160) = a(7): a18(161) = a(8): a18(162) = a(9):
Case 19
a18(163) = a(1): a18(164) = a(2): a18(165) = a(3):
a18(181) = a(4): a18(182) = a(5): a18(183) = a(6):
a18(199) = a(7): a18(200) = a(8): a18(201) = a(9):
Case 20
a18(166) = a(1): a18(167) = a(2): a18(168) = a(3):
a18(184) = a(4): a18(185) = a(5): a18(186) = a(6):
a18(202) = a(7): a18(203) = a(8): a18(204) = a(9):
Case 21
a18(169) = a(1): a18(170) = a(2): a18(171) = a(3):
a18(187) = a(4): a18(188) = a(5): a18(189) = a(6):
a18(205) = a(7): a18(206) = a(8): a18(207) = a(9):
Case 22
a18(172) = a(1): a18(173) = a(2): a18(174) = a(3):
a18(190) = a(4): a18(191) = a(5): a18(192) = a(6):
a18(208) = a(7): a18(209) = a(8): a18(210) = a(9):
Case 23
a18(175) = a(1): a18(176) = a(2): a18(177) = a(3):
a18(193) = a(4): a18(194) = a(5): a18(195) = a(6):
a18(211) = a(7): a18(212) = a(8): a18(213) = a(9):
Case 24
a18(178) = a(1): a18(179) = a(2): a18(180) = a(3):
a18(196) = a(4): a18(197) = a(5): a18(198) = a(6):
a18(214) = a(7): a18(215) = a(8): a18(216) = a(9):
Case 25
a18(217) = a(1): a18(218) = a(2): a18(219) = a(3):
a18(235) = a(4): a18(236) = a(5): a18(237) = a(6):
a18(253) = a(7): a18(254) = a(8): a18(255) = a(9):
Case 26
a18(220) = a(1): a18(221) = a(2): a18(222) = a(3):
a18(238) = a(4): a18(239) = a(5): a18(240) = a(6):
a18(256) = a(7): a18(257) = a(8): a18(258) = a(9):
Case 27
a18(223) = a(1): a18(224) = a(2): a18(225) = a(3):
a18(241) = a(4): a18(242) = a(5): a18(243) = a(6):
a18(259) = a(7): a18(260) = a(8): a18(261) = a(9):
Case 28
a18(226) = a(1): a18(227) = a(2): a18(228) = a(3):
a18(244) = a(4): a18(245) = a(5): a18(246) = a(6):
a18(262) = a(7): a18(263) = a(8): a18(264) = a(9):
Case 29
a18(229) = a(1): a18(230) = a(2): a18(231) = a(3):
a18(247) = a(4): a18(248) = a(5): a18(249) = a(6):
a18(265) = a(7): a18(266) = a(8): a18(267) = a(9):
Case 30
a18(232) = a(1): a18(233) = a(2): a18(234) = a(3):
a18(250) = a(4): a18(251) = a(5): a18(252) = a(6):
a18(268) = a(7): a18(269) = a(8): a18(270) = a(9):
Case 31
a18(271) = a(1): a18(272) = a(2): a18(273) = a(3):
a18(289) = a(4): a18(290) = a(5): a18(291) = a(6):
a18(307) = a(7): a18(308) = a(8): a18(309) = a(9):
Case 32
a18(274) = a(1): a18(275) = a(2): a18(276) = a(3):
a18(292) = a(4): a18(293) = a(5): a18(294) = a(6):
a18(310) = a(7): a18(311) = a(8): a18(312) = a(9):
Case 33
a18(277) = a(1): a18(278) = a(2): a18(279) = a(3):
a18(295) = a(4): a18(296) = a(5): a18(297) = a(6):
a18(313) = a(7): a18(314) = a(8): a18(315) = a(9):
Case 34
a18(280) = a(1): a18(281) = a(2): a18(282) = a(3):
a18(298) = a(4): a18(299) = a(5): a18(300) = a(6):
a18(316) = a(7): a18(317) = a(8): a18(318) = a(9):
Case 35
a18(283) = a(1): a18(284) = a(2): a18(285) = a(3):
a18(301) = a(4): a18(302) = a(5): a18(303) = a(6):
a18(319) = a(7): a18(320) = a(8): a18(321) = a(9):
Case 36
a18(286) = a(1): a18(287) = a(2): a18(288) = a(3):
a18(304) = a(4): a18(305) = a(5): a18(306) = a(6):
a18(322) = a(7): a18(323) = a(8): a18(324) = a(9):
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 = s18
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 9
a2 = a(j1)
For j2 = (1 + j1) To 9
If a2 = a(j2) Then fl1 = 0: Return
Next j2
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