' Constructs 14 x 14 Bordered Magic Squares (Distinct Integers)
' Composed Border, Almost Associated
' Tested with Office 365 under Windows 10
Sub MgcSqrs14e1()
Dim a1(196), a14(196), a(64), b1(196), b(196), c(48), a2(48), b2(196), c2(48)
y = MsgBox("Locked", vbCritical, "Routine MgcSqrs14e1")
End
n2 = 0: n3 = 0: k1 = 1: k2 = 1: n9 = 0: n10 = 0
ShtNm2 = "Cubes4"
' Generate Squares
Sheets("Klad1").Select
t1 = Timer
For j100 = 1 To 12 'Read
GoSub 2210 'Redefine Integers
' Read Semi Magic Corner Squares 4 x 4 (Simple Associated Magic Cubes)
For i1 = 1 To 64
a(i1) = Sheets(ShtNm2).Cells(j100, i1).Value
Next i1
n32 = 64: GoSub 900 'Remove used integers from available integers
' Store Semi Magic Corner Squares in a14()
a14(1) = a(1): a14(2) = a(2): a14(3) = a(3): a14(4) = a(4):
a14(15) = a(5): a14(16) = a(6): a14(17) = a(7): a14(18) = a(8):
a14(29) = a(9): a14(30) = a(10): a14(31) = a(11): a14(32) = a(12):
a14(43) = a(13): a14(44) = a(14): a14(45) = a(15): a14(46) = a(16):
a14(11) = a(17): a14(12) = a(18): a14(13) = a(19): a14(14) = a(20):
a14(25) = a(21): a14(26) = a(22): a14(27) = a(23): a14(28) = a(24):
a14(39) = a(25): a14(40) = a(26): a14(41) = a(27): a14(42) = a(28):
a14(53) = a(29): a14(54) = a(30): a14(55) = a(31): a14(56) = a(32):
a14(141) = a(33): a14(142) = a(34): a14(143) = a(35): a14(144) = a(36):
a14(155) = a(37): a14(156) = a(38): a14(157) = a(39): a14(158) = a(40):
a14(169) = a(41): a14(170) = a(42): a14(171) = a(43): a14(172) = a(44):
a14(183) = a(45): a14(184) = a(46): a14(185) = a(47): a14(186) = a(48):
a14(151) = a(49): a14(152) = a(50): a14(153) = a(51): a14(154) = a(52):
a14(165) = a(53): a14(166) = a(54): a14(167) = a(55): a14(168) = a(56):
a14(179) = a(57): a14(180) = a(58): a14(181) = a(59): a14(182) = a(60):
a14(193) = a(61): a14(194) = a(62): a14(195) = a(63): a14(196) = a(64):
' Restore available pairs in a1()
n10 = 0
For j1 = 1 To pmax
If b1(j1) <> 0 Then
n10 = n10 + 1
a1(n10) = b1(j1)
End If
Next j1
m1 = 1: m2 = n10: n10 = 0
GoSub 3000 'Magic rectangles (4 x 6)
If fl1 = 0 Then GoTo 1000
GoSub 850 'Double Check Identical Integers a11()
If fl1 = 1 Then
' n9 = n9 + 1: GoSub 1640 'Print results (lines)
n9 = n9 + 1: GoSub 1650 'Print results (squares)
End If
Erase b1, b, c
1000 Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s11)
y = MsgBox(t10, 0, "Routine MgcSqrs14e1")
End
' Determine Anti Magic Rectangle (4 x 6)
3000 Erase b2, c2: fl1 = 1
' Row 1
For jj24 = m1 To m2 'a2(24)
If b1(a1(jj24)) = 0 Then GoTo 240
If b2(a1(jj24)) = 0 Then b2(a1(jj24)) = a1(jj24): c2(24) = a1(jj24) Else GoTo 240
a2(24) = a1(jj24)
a2(25) = Pr4 - a2(24): If b2(a2(25)) = 0 Then b2(a2(25)) = a2(25): c2(25) = a2(25) Else GoTo 250
For jj23 = m1 To m2 'a2(23)
If b1(a1(jj23)) = 0 Then GoTo 230
If b2(a1(jj23)) = 0 Then b2(a1(jj23)) = a1(jj23): c2(23) = a1(jj23) Else GoTo 230
a2(23) = a1(jj23)
a2(26) = Pr4 - a2(23): If b2(a2(26)) = 0 Then b2(a2(26)) = a2(26): c2(26) = a2(26) Else GoTo 260
For jj22 = m1 To m2 'a2(22)
If b1(a1(jj22)) = 0 Then GoTo 220
If b2(a1(jj22)) = 0 Then b2(a1(jj22)) = a1(jj22): c2(22) = a1(jj22) Else GoTo 220
a2(22) = a1(jj22)
a2(27) = Pr4 - a2(22): If b2(a2(27)) = 0 Then b2(a2(27)) = a2(27): c2(27) = a2(27) Else GoTo 270
For jj21 = m1 To m2 'a2(21)
If b1(a1(jj21)) = 0 Then GoTo 210
If b2(a1(jj21)) = 0 Then b2(a1(jj21)) = a1(jj21): c2(21) = a1(jj21) Else GoTo 210
a2(21) = a1(jj21)
a2(28) = Pr4 - a2(21): If b2(a2(28)) = 0 Then b2(a2(28)) = a2(28): c2(28) = a2(28) Else GoTo 280
For jj20 = m1 To m2 'a2(20)
If b1(a1(jj20)) = 0 Then GoTo 200
If b2(a1(jj20)) = 0 Then b2(a1(jj20)) = a1(jj20): c2(20) = a1(jj20) Else GoTo 200
a2(20) = a1(jj20)
a2(29) = Pr4 - a2(20): If b2(a2(29)) = 0 Then b2(a2(29)) = a2(29): c2(29) = a2(29) Else GoTo 290
a2(19) = s6 - a2(20) - a2(21) - a2(22) - a2(23) - a2(24)
If a2(19) < a1(m1) Or a2(19) > a1(m2) Then GoTo 190
If b1(a2(19)) = 0 Then GoTo 190
If b2(a2(19)) = 0 Then b2(a2(19)) = a2(19): c2(19) = a2(19) Else GoTo 190
a2(30) = Pr4 - a2(19): If b2(a2(30)) = 0 Then b2(a2(30)) = a2(30): c2(30) = a2(30) Else GoTo 300
' Row 2
For jj18 = m2 To m1 Step -1 'a2(18)
If b1(a1(jj18)) = 0 Then GoTo 180
If b2(a1(jj18)) = 0 Then b2(a1(jj18)) = a1(jj18): c2(18) = a1(jj18) Else GoTo 180
a2(18) = a1(jj18)
a2(31) = Pr4 - a2(18): If b2(a2(31)) = 0 Then b2(a2(31)) = a2(31): c2(31) = a2(31) Else GoTo 310
For jj17 = m2 To m1 Step -1 'a2(17)
If b1(a1(jj17)) = 0 Then GoTo 170
If b2(a1(jj17)) = 0 Then b2(a1(jj17)) = a1(jj17): c2(17) = a1(jj17) Else GoTo 170
a2(17) = a1(jj17)
a2(32) = Pr4 - a2(17): If b2(a2(32)) = 0 Then b2(a2(32)) = a2(32): c2(32) = a2(32) Else GoTo 320
For jj16 = m2 To m1 Step -1 'a2(16)
If b1(a1(jj16)) = 0 Then GoTo 160
If b2(a1(jj16)) = 0 Then b2(a1(jj16)) = a1(jj16): c2(16) = a1(jj16) Else GoTo 160
a2(16) = a1(jj16)
a2(33) = Pr4 - a2(16): If b2(a2(33)) = 0 Then b2(a2(33)) = a2(33): c2(33) = a2(33) Else GoTo 330
For jj15 = m2 To m1 Step -1 'a2(15)
If b1(a1(jj15)) = 0 Then GoTo 150
If b2(a1(jj15)) = 0 Then b2(a1(jj15)) = a1(jj15): c2(15) = a1(jj15) Else GoTo 150
a2(15) = a1(jj15)
a2(34) = Pr4 - a2(15): If b2(a2(34)) = 0 Then b2(a2(34)) = a2(34): c2(34) = a2(34) Else GoTo 340
For jj14 = m2 To m1 Step -1 'a2(14)
If b1(a1(jj14)) = 0 Then GoTo 140
If b2(a1(jj14)) = 0 Then b2(a1(jj14)) = a1(jj14): c2(14) = a1(jj14) Else GoTo 140
a2(14) = a1(jj14)
a2(35) = Pr4 - a2(14): If b2(a2(35)) = 0 Then b2(a2(35)) = a2(35): c2(35) = a2(35) Else GoTo 350
a2(13) = s6 - a2(14) - a2(15) - a2(16) - a2(17) - a2(18)
If a2(13) < a1(m1) Or a2(13) > a1(m2) Then GoTo 130
If b1(a2(13)) = 0 Then GoTo 130
If b2(a2(13)) = 0 Then b2(a2(13)) = a2(13): c2(13) = a2(13) Else GoTo 130
a2(36) = Pr4 - a2(13): If b2(a2(36)) = 0 Then b2(a2(36)) = a2(36): c2(36) = a2(36) Else GoTo 360
' Row 3
For jj12 = (m2 / 2 - 12) To (m2 / 2 + 12) 'a2(12)
If b1(a1(jj12)) = 0 Then GoTo 120
If b2(a1(jj12)) = 0 Then b2(a1(jj12)) = a1(jj12): c2(12) = a1(jj12) Else GoTo 120
a2(12) = a1(jj12)
a2(37) = Pr4 - a2(12): If b2(a2(37)) = 0 Then b2(a2(37)) = a2(37): c2(37) = a2(37) Else GoTo 370
a2(6) = s4 - a2(12) - a2(18) - a2(24)
If a2(6) < a1(m1) Or a2(6) > a1(m2) Then GoTo 60
If b1(a2(6)) = 0 Then GoTo 60
If b2(a2(6)) = 0 Then b2(a2(6)) = a2(6): c2(6) = a2(6) Else GoTo 60
a2(43) = Pr4 - a2(6): If b2(a2(43)) = 0 Then b2(a2(43)) = a2(43): c2(43) = a2(43) Else GoTo 430
For jj11 = (m2 / 2 - 12) To (m2 / 2 + 12) 'a2(11)
If b1(a1(jj11)) = 0 Then GoTo 110
If b2(a1(jj11)) = 0 Then b2(a1(jj11)) = a1(jj11): c2(11) = a1(jj11) Else GoTo 110
a2(11) = a1(jj11)
a2(38) = Pr4 - a2(11): If b2(a2(38)) = 0 Then b2(a2(38)) = a2(38): c2(38) = a2(38) Else GoTo 380
a2(5) = s4 - a2(11) - a2(17) - a2(23)
If a2(5) < a1(m1) Or a2(5) > a1(m2) Then GoTo 50
If b1(a2(5)) = 0 Then GoTo 50
If b2(a2(5)) = 0 Then b2(a2(5)) = a2(5): c2(5) = a2(5) Else GoTo 50
a2(44) = Pr4 - a2(5): If b2(a2(44)) = 0 Then b2(a2(44)) = a2(44): c2(44) = a2(44) Else GoTo 440
For jj10 = (m2 / 2 - 12) To (m2 / 2 + 12) 'a2(10)
If b1(a1(jj10)) = 0 Then GoTo 100
If b2(a1(jj10)) = 0 Then b2(a1(jj10)) = a1(jj10): c2(10) = a1(jj10) Else GoTo 100
a2(10) = a1(jj10)
a2(39) = Pr4 - a2(10): If b2(a2(39)) = 0 Then b2(a2(39)) = a2(39): c2(39) = a2(39) Else GoTo 390
a2(4) = s4 - a2(10) - a2(16) - a2(22)
If a2(4) < a1(m1) Or a2(4) > a1(m2) Then GoTo 40
If b1(a2(4)) = 0 Then GoTo 40
If b2(a2(4)) = 0 Then b2(a2(4)) = a2(4): c2(4) = a2(4) Else GoTo 40
a2(45) = Pr4 - a2(4): If b2(a2(45)) = 0 Then b2(a2(45)) = a2(45): c2(45) = a2(45) Else GoTo 450
For jj9 = (m2 / 2 - 12) To (m2 / 2 + 12) 'a2(9)
If b1(a1(jj9)) = 0 Then GoTo 90
If b2(a1(jj9)) = 0 Then b2(a1(jj9)) = a1(jj9): c2(9) = a1(jj9) Else GoTo 90
a2(9) = a1(jj9)
a2(40) = Pr4 - a2(9): If b2(a2(40)) = 0 Then b2(a2(40)) = a2(40): c2(40) = a2(40) Else GoTo 400
a2(3) = s4 - a2(9) - a2(15) - a2(21)
If a2(3) < a1(m1) Or a2(3) > a1(m2) Then GoTo 30
If b1(a2(3)) = 0 Then GoTo 30
If b2(a2(3)) = 0 Then b2(a2(3)) = a2(3): c2(3) = a2(3) Else GoTo 30
a2(46) = Pr4 - a2(3): If b2(a2(46)) = 0 Then b2(a2(46)) = a2(46): c2(46) = a2(46) Else GoTo 460
For jj8 = (m2 / 2 - 12) To (m2 / 2 + 12) 'a2(8)
If b1(a1(jj8)) = 0 Then GoTo 80
If b2(a1(jj8)) = 0 Then b2(a1(jj8)) = a1(jj8): c2(8) = a1(jj8) Else GoTo 80
a2(8) = a1(jj8)
a2(41) = Pr4 - a2(8): If b2(a2(41)) = 0 Then b2(a2(41)) = a2(41): c2(41) = a2(41) Else GoTo 410
a2(2) = s4 - a2(8) - a2(14) - a2(20)
If a2(2) < a1(m1) Or a2(2) > a1(m2) Then GoTo 20
If b1(a2(2)) = 0 Then GoTo 20
If b2(a2(2)) = 0 Then b2(a2(2)) = a2(2): c2(2) = a2(2) Else GoTo 20
a2(47) = Pr4 - a2(2): If b2(a2(47)) = 0 Then b2(a2(47)) = a2(47): c2(47) = a2(47) Else GoTo 470
a2(7) = s6 - a2(8) - a2(9) - a2(10) - a2(11) - a2(12)
If a2(7) < a1(m1) Or a2(7) > a1(m2) Then GoTo 70
If b1(a2(7)) = 0 Then GoTo 70
If b2(a2(7)) = 0 Then b2(a2(7)) = a2(7): c2(7) = a2(7) Else GoTo 70
a2(42) = Pr4 - a2(7): If b2(a2(42)) = 0 Then b2(a2(42)) = a2(42): c2(42) = a2(42) Else GoTo 420
a2(1) = s4 - a2(7) - a2(13) - a2(19)
If a2(1) < a1(m1) Or a2(1) > a1(m2) Then GoTo 10
If b1(a2(1)) = 0 Then GoTo 10
If b2(a2(1)) = 0 Then b2(a2(1)) = a2(1): c2(1) = a2(1) Else GoTo 10
a2(48) = Pr4 - a2(1): If b2(a2(48)) = 0 Then b2(a2(48)) = a2(48): c2(48) = a2(48) Else GoTo 480
n10 = n10 + 1
Select Case n10
Case 1
a14(5) = a2(1): a14(6) = a2(2): a14(7) = a2(3): a14(8) = a2(4): a14(9) = a2(5): a14(10) = a2(6):
a14(19) = a2(7): a14(20) = a2(8): a14(21) = a2(9): a14(22) = a2(10): a14(23) = a2(11): a14(24) = a2(12):
a14(33) = a2(13): a14(34) = a2(14): a14(35) = a2(15): a14(36) = a2(16): a14(37) = a2(17): a14(38) = a2(18):
a14(47) = a2(19): a14(48) = a2(20): a14(49) = a2(21): a14(50) = a2(22): a14(51) = a2(23): a14(52) = a2(24):
a14(145) = a2(25): a14(146) = a2(26): a14(147) = a2(27): a14(148) = a2(28): a14(149) = a2(29): a14(150) = a2(30):
a14(159) = a2(31): a14(160) = a2(32): a14(161) = a2(33): a14(162) = a2(34): a14(163) = a2(35): a14(164) = a2(36):
a14(173) = a2(37): a14(174) = a2(38): a14(175) = a2(39): a14(176) = a2(40): a14(177) = a2(41): a14(178) = a2(42):
a14(187) = a2(43): a14(188) = a2(44): a14(189) = a2(45): a14(190) = a2(46): a14(191) = a2(47): a14(192) = a2(48):
n32 = 24: GoSub 910 'Remove used pairs from b1()
Erase b2, c2: GoTo 240
Case 2
a14(127) = a2(1): a14(113) = a2(2): a14(99) = a2(3): a14(85) = a2(4): a14(71) = a2(5): a14(57) = a2(6):
a14(128) = a2(7): a14(114) = a2(8): a14(100) = a2(9): a14(86) = a2(10): a14(72) = a2(11): a14(58) = a2(12):
a14(129) = a2(13): a14(115) = a2(14): a14(101) = a2(15): a14(87) = a2(16): a14(73) = a2(17): a14(59) = a2(18):
a14(130) = a2(19): a14(116) = a2(20): a14(102) = a2(21): a14(88) = a2(22): a14(74) = a2(23): a14(60) = a2(24):
a14(137) = a2(25): a14(123) = a2(26): a14(109) = a2(27): a14(95) = a2(28): a14(81) = a2(29): a14(67) = a2(30):
a14(138) = a2(31): a14(124) = a2(32): a14(110) = a2(33): a14(96) = a2(34): a14(82) = a2(35): a14(68) = a2(36):
a14(139) = a2(37): a14(125) = a2(38): a14(111) = a2(39): a14(97) = a2(40): a14(83) = a2(41): a14(69) = a2(42):
a14(140) = a2(43): a14(126) = a2(44): a14(112) = a2(45): a14(98) = a2(46): a14(84) = a2(47): a14(70) = a2(48):
Erase b2, c2: Return
End Select
b2(c2(48)) = 0: c2(48) = 0
480 b2(c2(1)) = 0: c2(1) = 0
10 b2(c2(42)) = 0: c2(42) = 0
420 b2(c2(7)) = 0: c2(7) = 0
70 b2(c2(47)) = 0: c2(47) = 0
470 b2(c2(2)) = 0: c2(2) = 0
20 b2(c2(41)) = 0: c2(41) = 0
410 b2(c2(8)) = 0: c2(8) = 0
80 Next jj8
b2(c2(46)) = 0: c2(46) = 0
460 b2(c2(3)) = 0: c2(3) = 0
30 b2(c2(40)) = 0: c2(40) = 0
400 b2(c2(9)) = 0: c2(9) = 0
90 Next jj9
b2(c2(45)) = 0: c2(45) = 0
450 b2(c2(4)) = 0: c2(4) = 0
40 b2(c2(39)) = 0: c2(39) = 0
390 b2(c2(10)) = 0: c2(10) = 0
100 Next jj10
b2(c2(44)) = 0: c2(44) = 0
440 b2(c2(5)) = 0: c2(5) = 0
50 b2(c2(38)) = 0: c2(38) = 0
380 b2(c2(11)) = 0: c2(11) = 0
110 Next jj11
b2(c2(43)) = 0: c2(43) = 0
430 b2(c2(6)) = 0: c2(6) = 0
60 b2(c2(37)) = 0: c2(37) = 0
370 b2(c2(12)) = 0: c2(12) = 0
120 Next jj12
b2(c2(36)) = 0: c2(36) = 0
360 b2(c2(13)) = 0: c2(13) = 0
130 b2(c2(35)) = 0: c2(35) = 0
350 b2(c2(14)) = 0: c2(14) = 0
140 Next jj14
b2(c2(34)) = 0: c2(34) = 0
340 b2(c2(15)) = 0: c2(15) = 0
150 Next jj15
b2(c2(33)) = 0: c2(33) = 0
330 b2(c2(16)) = 0: c2(16) = 0
160 Next jj16
b2(c2(32)) = 0: c2(32) = 0
320 b2(c2(17)) = 0: c2(17) = 0
170 Next jj17
b2(c2(31)) = 0: c2(31) = 0
310 b2(c2(18)) = 0: c2(18) = 0
180 Next jj18
b2(c2(30)) = 0: c2(30) = 0
300 b2(c2(19)) = 0: c2(19) = 0
190 b2(c2(29)) = 0: c2(29) = 0
290 b2(c2(20)) = 0: c2(20) = 0
200 Next jj20
b2(c2(28)) = 0: c2(28) = 0
280 b2(c2(21)) = 0: c2(21) = 0
210 Next jj21
b2(c2(27)) = 0: c2(27) = 0
270 b2(c2(22)) = 0: c2(22) = 0
220 Next jj22
b2(c2(26)) = 0: c2(26) = 0
260 b2(c2(23)) = 0: c2(23) = 0
230 Next jj23
b2(c2(25)) = 0: c2(25) = 0
250 b2(c2(24)) = 0: c2(24) = 0
240 Next jj24
fl1 = 0
Return
' Print results (Selecyted Integers)
1640
For i1 = 1 To 196
Cells(n9, i1).Value = a14(i1)
Next i1
Cells(n9, 197).Value = s14
Cells(n9, 198).Value = j100
Return
' Print results (Squares)
1650 n2 = n2 + 1
If n2 = 3 Then
n2 = 1: k1 = k1 + 15: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 15
End If
Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = j100
i3 = 0
For i1 = 1 To 14
For i2 = 1 To 14
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a14(i3)
Next i2
Next i1
Return
' Assign Center Square a()
700 a14(61) = a(1): a14(62) = a(2): a14(63) = a(3): a14(64) = a(4): a14(65) = a(5): a14(66) = a(6):
a14(75) = a(7): a14(76) = a(8): a14(77) = a(9): a14(78) = a(10): a14(79) = a(11): a14(80) = a(12):
a14(89) = a(13): a14(90) = a(14): a14(91) = a(15): a14(92) = a(16): a14(93) = a(17): a14(94) = a(18):
a14(103) = a(19): a14(104) = a(20): a14(105) = a(21): a14(106) = a(22): a14(107) = a(23): a14(108) = a(24):
a14(117) = a(25): a14(118) = a(26): a14(119) = a(27): a14(120) = a(28): a14(121) = a(29): a14(122) = a(30):
a14(131) = a(31): a14(132) = a(32): a14(133) = a(33): a14(134) = a(34): a14(135) = a(35): a14(136) = a(36):
Return
' Double Check Identical Numbers Center Square a()
800 fl1 = 1
For i1 = 1 To 36
a20 = a(i1): If a20 = 0 Then GoTo 810
For i2 = (1 + i1) To 36
If a20 = a(i2) Then fl1 = 0: Return
Next i2
810 Next i1
Return
' Exclude solutions with identical numbers a14()
850 fl1 = 1
For j1 = 1 To 196
a20 = a14(j1): If a20 = 0 Then GoTo 855
For j2 = (1 + j1) To 196
If a20 = a14(j2) Then fl1 = 0: Return
Next j2
855 Next j1
Return
' Remove used integers a() from available integers b1()
900 For i1 = 1 To n32
b1(a(i1)) = 0
Next i1
Return
' Remove used integers a2() from available integers b1()
910 For i1 = 1 To n32
b1(a2(i1)) = 0: b1(Pr4 - a2(i1)) = 0
Next i1
Return
' Define integer
2210 Pr4 = 197: nVar = 196
n10 = 0
s6 = 3 * Pr4 'MC6
s4 = 2 * Pr4 'MC4
s14 = 1379 'MC14
Erase b1
For i1 = 1 To nVar
a1(i1) = i1: b1(i1) = i1
Next i1
m1 = 1: m2 = nVar: pmax = 196
Return
End Sub