' Generates Inlaid Magic Squares of order 15
' Based on Order 3 Magic Sub Squares with Different Magic Sums
' Tested with Office 2007 under Windows 7
Sub Priem15k4()
Dim a1(750), a(25), a15(225), b1(48355), b(48355), c(25)
y = MsgBox("Locked", vbCritical, "Routine Priem15k4")
End
n1 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
Sheets("Klad1").Select
t1 = Timer
For j200 = 2 To 1292
s15 = Sheets("Input15").Cells(j200, 52).Value
Erase a15
'Define Center Elements
For i1 = 1 To 25
a(i1) = Sheets("Input15").Cells(j200, i1).Value
Next i1
a15(17) = a(1): a15(20) = a(2): a15(23) = a(3): a15(26) = a(4): a15(29) = a(5):
a15(62) = a(6): a15(65) = a(7): a15(68) = a(8): a15(71) = a(9): a15(74) = a(10):
a15(107) = a(11): a15(110) = a(12): a15(113) = a(13): a15(116) = a(14): a15(119) = a(15):
a15(152) = a(16): a15(155) = a(17): a15(158) = a(18): a15(161) = a(19): a15(164) = a(20):
a15(197) = a(21): a15(200) = a(22): a15(203) = a(23): a15(206) = a(24): a15(209) = a(25):
Erase a
For j101 = 27 To 51
j100 = Sheets("Input15").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 225
b1(a15(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 a15()
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 = 25 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 Priem15k4")
End
' Assign to a15()
750 Select Case n10
Case 1
a15(1) = a(1): a15(2) = a(2): a15(3) = a(3):
a15(16) = a(4): a15(17) = a(5): a15(18) = a(6):
a15(31) = a(7): a15(32) = a(8): a15(33) = a(9):
Case 2
a15(4) = a(1): a15(5) = a(2): a15(6) = a(3):
a15(19) = a(4): a15(20) = a(5): a15(21) = a(6):
a15(34) = a(7): a15(35) = a(8): a15(36) = a(9):
Case 3
a15(7) = a(1): a15(8) = a(2): a15(9) = a(3):
a15(22) = a(4): a15(23) = a(5): a15(24) = a(6):
a15(37) = a(7): a15(38) = a(8): a15(39) = a(9):
Case 4
a15(10) = a(1): a15(11) = a(2): a15(12) = a(3):
a15(25) = a(4): a15(26) = a(5): a15(27) = a(6):
a15(40) = a(7): a15(41) = a(8): a15(42) = a(9):
Case 5
a15(13) = a(1): a15(14) = a(2): a15(15) = a(3):
a15(28) = a(4): a15(29) = a(5): a15(30) = a(6):
a15(43) = a(7): a15(44) = a(8): a15(45) = a(9):
Case 6
a15(46) = a(1): a15(47) = a(2): a15(48) = a(3):
a15(61) = a(4): a15(62) = a(5): a15(63) = a(6):
a15(76) = a(7): a15(77) = a(8): a15(78) = a(9):
Case 7
a15(49) = a(1): a15(50) = a(2): a15(51) = a(3):
a15(64) = a(4): a15(65) = a(5): a15(66) = a(6):
a15(79) = a(7): a15(80) = a(8): a15(81) = a(9):
Case 8
a15(52) = a(1): a15(53) = a(2): a15(54) = a(3):
a15(67) = a(4): a15(68) = a(5): a15(69) = a(6):
a15(82) = a(7): a15(83) = a(8): a15(84) = a(9):
Case 9
a15(55) = a(1): a15(56) = a(2): a15(57) = a(3):
a15(70) = a(4): a15(71) = a(5): a15(72) = a(6):
a15(85) = a(7): a15(86) = a(8): a15(87) = a(9):
Case 10
a15(58) = a(1): a15(59) = a(2): a15(60) = a(3):
a15(73) = a(4): a15(74) = a(5): a15(75) = a(6):
a15(88) = a(7): a15(89) = a(8): a15(90) = a(9):
Case 11
a15(91) = a(1): a15(92) = a(2): a15(93) = a(3):
a15(106) = a(4): a15(107) = a(5): a15(108) = a(6):
a15(121) = a(7): a15(122) = a(8): a15(123) = a(9):
Case 12
a15(94) = a(1): a15(95) = a(2): a15(96) = a(3):
a15(109) = a(4): a15(110) = a(5): a15(111) = a(6):
a15(124) = a(7): a15(125) = a(8): a15(126) = a(9):
Case 13
a15(97) = a(1): a15(98) = a(2): a15(99) = a(3):
a15(112) = a(4): a15(113) = a(5): a15(114) = a(6):
a15(127) = a(7): a15(128) = a(8): a15(129) = a(9):
Case 14
a15(100) = a(1): a15(101) = a(2): a15(102) = a(3):
a15(115) = a(4): a15(116) = a(5): a15(117) = a(6):
a15(130) = a(7): a15(131) = a(8): a15(132) = a(9):
Case 15
a15(103) = a(1): a15(104) = a(2): a15(105) = a(3):
a15(118) = a(4): a15(119) = a(5): a15(120) = a(6):
a15(133) = a(7): a15(134) = a(8): a15(135) = a(9):
Case 16
a15(136) = a(1): a15(137) = a(2): a15(138) = a(3):
a15(151) = a(4): a15(152) = a(5): a15(153) = a(6):
a15(166) = a(7): a15(167) = a(8): a15(168) = a(9):
Case 17
a15(139) = a(1): a15(140) = a(2): a15(141) = a(3):
a15(154) = a(4): a15(155) = a(5): a15(156) = a(6):
a15(169) = a(7): a15(170) = a(8): a15(171) = a(9):
Case 18
a15(142) = a(1): a15(143) = a(2): a15(144) = a(3):
a15(157) = a(4): a15(158) = a(5): a15(159) = a(6):
a15(172) = a(7): a15(173) = a(8): a15(174) = a(9):
Case 19
a15(145) = a(1): a15(146) = a(2): a15(147) = a(3):
a15(160) = a(4): a15(161) = a(5): a15(162) = a(6):
a15(175) = a(7): a15(176) = a(8): a15(177) = a(9):
Case 20
a15(148) = a(1): a15(149) = a(2): a15(150) = a(3):
a15(163) = a(4): a15(164) = a(5): a15(165) = a(6):
a15(178) = a(7): a15(179) = a(8): a15(180) = a(9):
Case 21
a15(181) = a(1): a15(182) = a(2): a15(183) = a(3):
a15(196) = a(4): a15(197) = a(5): a15(198) = a(6):
a15(211) = a(7): a15(212) = a(8): a15(213) = a(9):
Case 22
a15(184) = a(1): a15(185) = a(2): a15(186) = a(3):
a15(199) = a(4): a15(200) = a(5): a15(201) = a(6):
a15(214) = a(7): a15(215) = a(8): a15(216) = a(9):
Case 23
a15(187) = a(1): a15(188) = a(2): a15(189) = a(3):
a15(202) = a(4): a15(203) = a(5): a15(204) = a(6):
a15(217) = a(7): a15(218) = a(8): a15(219) = a(9):
Case 24
a15(190) = a(1): a15(191) = a(2): a15(192) = a(3):
a15(205) = a(4): a15(206) = a(5): a15(207) = a(6):
a15(220) = a(7): a15(221) = a(8): a15(222) = a(9):
Case 25
a15(193) = a(1): a15(194) = a(2): a15(195) = a(3):
a15(208) = a(4): a15(209) = a(5): a15(210) = a(6):
a15(223) = a(7): a15(224) = a(8): a15(225) = a(9):
End Select
Return
' Print results: squares a15()
660 n1 = n1 + 1
If n1 = 2 Then
n1 = 1: k1 = k1 + 16: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 16
End If
Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = s15
i3 = 0
For i1 = 1 To 15
For i2 = 1 To 15
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a15(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 a15()
850 fl1 = 1
For j1 = 1 To 225
a2 = a15(j1): If a2 = 0 Then GoTo 860
For j2 = (1 + j1) To 225
If a2 = a15(j2) Then fl1 = 0: Return
Next j2
860 Next j1
Return
End Sub