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