' Generates Concentric Magic Squares of order 11 with 4 x 5 Even Corner Numbers
' Suitable for Concentric Lozenge Squares of order 15 (Predefined Borders)
' Tested with Office 2007 under Windows 7
Sub MgcSqr11a2()
Dim a1(121), a(121), b1(221), b(221), c(121), a9(81)
y = MsgBox("Locked", vbCritical, "Routine MgcSqr11b1")
End
n2 = 0: n3 = 0: k1 = 1: k2 = 1: n9 = 0
Sheets("Klad1").Select
t1 = Timer
For j100 = 1 To 48
' Define Variables
a1(1) = 5: a1(2) = 6: a1(3) = 7: a1(4) = 8: a1(5) = 9: a1(6) = 10:
a1(7) = 11: a1(8) = 19: a1(9) = 21: a1(10) = 22:
a1(11) = 23: a1(12) = 24: a1(13) = 25: a1(14) = 27: a1(15) = 33:
a1(16) = 35: a1(17) = 37: a1(18) = 38: a1(19) = 39:
a1(20) = 41: a1(21) = 43: a1(22) = 47: a1(23) = 49: a1(24) = 51:
a1(25) = 53: a1(26) = 55: a1(27) = 57: a1(28) = 59:
a1(29) = 61: a1(30) = 63: a1(31) = 65: a1(32) = 67: a1(33) = 69:
a1(34) = 71: a1(35) = 73: a1(36) = 74: a1(37) = 75:
a1(38) = 76: a1(39) = 77: a1(40) = 79: a1(41) = 81: a1(42) = 83:
a1(43) = 85: a1(44) = 87: a1(45) = 89: a1(46) = 91:
a1(47) = 92: a1(48) = 93: a1(49) = 94: a1(50) = 95: a1(51) = 97:
a1(52) = 99: a1(53) = 101: a1(54) = 103: a1(55) = 105:
a1(56) = 106: a1(57) = 107: a1(58) = 108: a1(59) = 109: a1(60) = 111:
a1(61) = 113: a1(62) = 115: a1(63) = 117: a1(64) = 118:
a1(65) = 119: a1(66) = 120: a1(67) = 121: a1(68) = 123: a1(69) = 125:
a1(70) = 127: a1(71) = 129: a1(72) = 131: a1(73) = 132:
a1(74) = 133: a1(75) = 134: a1(76) = 135: a1(77) = 137: a1(78) = 139:
a1(79) = 141: a1(80) = 143: a1(81) = 145: a1(82) = 147:
a1(83) = 149: a1(84) = 150: a1(85) = 151: a1(86) = 152: a1(87) = 153:
a1(88) = 155: a1(89) = 157: a1(90) = 159: a1(91) = 161:
a1(92) = 163: a1(93) = 165: a1(94) = 167: a1(95) = 169: a1(96) = 171:
a1(97) = 173: a1(98) = 175: a1(99) = 177: a1(100) = 179:
a1(101) = 183: a1(102) = 185: a1(103) = 187: a1(104) = 188: a1(105) = 189:
a1(106) = 191: a1(107) = 193: a1(108) = 199: a1(109) = 201:
a1(110) = 202: a1(111) = 203: a1(112) = 204: a1(113) = 205: a1(114) = 207:
a1(115) = 215: a1(116) = 216: a1(117) = 217: a1(118) = 218:
a1(119) = 219: a1(120) = 220: a1(121) = 221:
s1 = 1243: Pr11 = 226: m1 = 1: m2 = 121
aMin = a1(1): aMax = a1(121)
Erase b1
For i1 = m1 To m2
b1(a1(i1)) = a1(i1)
Next i1
' Raed Center Square, Define Border Integers
For i1 = 1 To 81
x = Sheets("Center9").Cells(j100, i1)
a9(i1) = x: b1(x) = 0
Next i1
a(13) = a9(1): a(14) = a9(2): a(15) = a9(3): a(16) = a9(4): a(17) = a9(5):
a(18) = a9(6): a(19) = a9(7): a(20) = a9(8): a(21) = a9(9):
a(24) = a9(10): a(25) = a9(11): a(26) = a9(12): a(27) = a9(13): a(28) = a9(14):
a(29) = a9(15): a(30) = a9(16): a(31) = a9(17): a(32) = a9(18):
a(35) = a9(19): a(36) = a9(20): a(37) = a9(21): a(38) = a9(22): a(39) = a9(23):
a(40) = a9(24): a(41) = a9(25): a(42) = a9(26): a(43) = a9(27):
a(46) = a9(28): a(47) = a9(29): a(48) = a9(30): a(49) = a9(31): a(50) = a9(32):
a(51) = a9(33): a(52) = a9(34): a(53) = a9(35): a(54) = a9(36):
a(57) = a9(37): a(58) = a9(38): a(59) = a9(39): a(60) = a9(40): a(61) = a9(41):
a(62) = a9(42): a(63) = a9(43): a(64) = a9(44): a(65) = a9(45):
a(68) = a9(46): a(69) = a9(47): a(70) = a9(48): a(71) = a9(49): a(72) = a9(50):
a(73) = a9(51): a(74) = a9(52): a(75) = a9(53): a(76) = a9(54):
a(79) = a9(55): a(80) = a9(56): a(81) = a9(57): a(82) = a9(58): a(83) = a9(59):
a(84) = a9(60): a(85) = a9(61): a(86) = a9(62): a(87) = a9(63):
a(90) = a9(64): a(91) = a9(65): a(92) = a9(66): a(93) = a9(67): a(94) = a9(68):
a(95) = a9(69): a(96) = a9(70): a(97) = a9(71): a(98) = a9(72):
a(101) = a9(73): a(102) = a9(74): a(103) = a9(75): a(104) = a9(76): a(105) = a9(77):
a(106) = a9(78): a(107) = a9(79): a(108) = a9(80): a(109) = a9(81):
i2 = 0
For i1 = aMin To aMax
If b1(i1) <> 0 Then i2 = i2 + 1: a1(i2) = b1(i1)
Next i1
m1 = 1: m2 = i2:
' Generate Border(s)
For j118 = m2 To m1 Step -1 'a(118) odd
If CInt(a1(j118) / 2) = a1(j118) / 2 Then GoTo 1180
If b(a1(j118)) = 0 Then b(a1(j118)) = a1(j118): c(118) = a1(j118) Else GoTo 1180
a(118) = a1(j118)
a(8) = Pr11 - a(118): If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 80
For j117 = j118 - 1 To m1 Step -1 'a(117) odd
If CInt(a1(j117) / 2) = a1(j117) / 2 Then GoTo 1170
If b(a1(j117)) = 0 Then b(a1(j117)) = a1(j117): c(117) = a1(j117) Else GoTo 1170
a(117) = a1(j117)
a(7) = Pr11 - a(117): If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 70
For j116 = j117 - 1 To m1 Step -1 'a(116) odd
If CInt(a1(j116) / 2) = a1(j116) / 2 Then GoTo 1160
If b(a1(j116)) = 0 Then b(a1(j116)) = a1(j116): c(116) = a1(j116) Else GoTo 1160
a(116) = a1(j116)
a(6) = Pr11 - a(116): If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 60
For j115 = j116 - 1 To m1 Step -1 'a(115) odd
If CInt(a1(j115) / 2) = a1(j115) / 2 Then GoTo 1150
If b(a1(j115)) = 0 Then b(a1(j115)) = a1(j115): c(115) = a1(j115) Else GoTo 1150
a(115) = a1(j115)
a(5) = Pr11 - a(115): If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 50
For j114 = j115 - 1 To m1 Step -1 'a(114) odd
If CInt(a1(j114) / 2) = a1(j114) / 2 Then GoTo 1140
If b(a1(j114)) = 0 Then b(a1(j114)) = a1(j114): c(114) = a1(j114) Else GoTo 1140
a(114) = a1(j114)
a(4) = Pr11 - a(114): If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 40
For j119 = m2 To m1 Step -1 'a(119) even
If CInt(a1(j119) / 2) <> a1(j119) / 2 Then GoTo 1190
If b(a1(j119)) = 0 Then b(a1(j119)) = a1(j119): c(119) = a1(j119) Else GoTo 1190
a(119) = a1(j119)
a(9) = Pr11 - a(119): If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 90
For j120 = j119 - 1 To m1 Step -1 'a(120) even
If CInt(a1(j120) / 2) <> a1(j120) / 2 Then GoTo 1200
If b(a1(j120)) = 0 Then b(a1(j120)) = a1(j120): c(120) = a1(j120) Else GoTo 1200
a(120) = a1(j120)
a(10) = Pr11 - a(120): If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 100
For j121 = j120 - 1 To m1 Step -1 'a(121) even
If CInt(a1(j121) / 2) <> a1(j121) / 2 Then GoTo 1210
If b(a1(j121)) = 0 Then b(a1(j121)) = a1(j121): c(121) = a1(j121) Else GoTo 1210
a(121) = a1(j121)
a(1) = Pr11 - a(121): If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 10
For j112 = j121 - 1 To m1 Step -1 'a(112) even
If CInt(a1(j112) / 2) <> a1(j112) / 2 Then GoTo 1120
If b(a1(j112)) = 0 Then b(a1(j112)) = a1(j112): c(112) = a1(j112) Else GoTo 1120
a(112) = a1(j112)
a(2) = Pr11 - a(112): If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 20
For j113 = j112 - 1 To m1 Step -1 'a(113) even
If CInt(a1(j113) / 2) <> a1(j113) / 2 Then GoTo 1130
If b(a1(j113)) = 0 Then b(a1(j113)) = a1(j113): c(113) = a1(j113) Else GoTo 1130
a(113) = a1(j113)
a(3) = Pr11 - a(113): If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 30
a(111) = s1 - a(112) - a(113) - a(114) - a(115) - a(116) - a(117) - a(118) - a(119) - a(120) - a(121)
If a(111) < a1(m1) Or a(111) > a1(m2) Then GoTo 1110:
If b1(a(111)) = 0 Then GoTo 1110
If b(a(111)) = 0 Then b(a(111)) = a(111): c(111) = a(111) Else GoTo 1110
a(11) = Pr11 - a(111): If b(a(11)) = 0 Then b(a(11)) = a(11): c(11) = a(11) Else GoTo 110
For j88 = m2 To m1 Step -1 'a(88) odd
If CInt(a1(j88) / 2) = a1(j88) / 2 Then GoTo 880
If b(a1(j88)) = 0 Then b(a1(j88)) = a1(j88): c(88) = a1(j88) Else GoTo 880
a(88) = a1(j88)
a(78) = Pr11 - a(88): If b(a(78)) = 0 Then b(a(78)) = a(78): c(78) = a(78) Else GoTo 780
For j77 = j88 - 1 To m1 Step -1 'a(77) odd
If CInt(a1(j77) / 2) = a1(j77) / 2 Then GoTo 770
If b(a1(j77)) = 0 Then b(a1(j77)) = a1(j77): c(77) = a1(j77) Else GoTo 770
a(77) = a1(j77)
a(67) = Pr11 - a(77): If b(a(67)) = 0 Then b(a(67)) = a(67): c(67) = a(67) Else GoTo 670
For j66 = j77 - 1 To m1 Step -1 'a(66) odd
If CInt(a1(j66) / 2) = a1(j66) / 2 Then GoTo 660
If b(a1(j66)) = 0 Then b(a1(j66)) = a1(j66): c(66) = a1(j66) Else GoTo 660
a(66) = a1(j66)
a(56) = Pr11 - a(66): If b(a(56)) = 0 Then b(a(56)) = a(56): c(56) = a(56) Else GoTo 560
For j55 = j66 - 1 To m1 Step -1 'a(55) odd
If CInt(a1(j55) / 2) = a1(j55) / 2 Then GoTo 550
If b(a1(j55)) = 0 Then b(a1(j55)) = a1(j55): c(55) = a1(j55) Else GoTo 550
a(55) = a1(j55)
a(45) = Pr11 - a(55): If b(a(45)) = 0 Then b(a(45)) = a(45): c(45) = a(45) Else GoTo 450
For j44 = j55 - 1 To m1 Step -1 'a(44) odd
If CInt(a1(j44) / 2) = a1(j44) / 2 Then GoTo 440
If b(a1(j44)) = 0 Then b(a1(j44)) = a1(j44): c(44) = a1(j44) Else GoTo 440
a(44) = a1(j44)
a(34) = Pr11 - a(44): If b(a(34)) = 0 Then b(a(34)) = a(34): c(34) = a(34) Else GoTo 340
' Remainder even
For j110 = m1 To m2 'a(110)
If b(a1(j110)) = 0 Then b(a1(j110)) = a1(j110): c(110) = a1(j110) Else GoTo 1100
a(110) = a1(j110)
a(100) = Pr11 - a(110): If b(a(100)) = 0 Then b(a(100)) = a(100): c(100) = a(100) Else GoTo 1000
For j99 = j110 + 1 To m2 'a(99)
If b(a1(j99)) = 0 Then b(a1(j99)) = a1(j99): c(99) = a1(j99) Else GoTo 990
a(99) = a1(j99)
a(89) = Pr11 - a(99): If b(a(89)) = 0 Then b(a(89)) = a(89): c(89) = a(89) Else GoTo 890
For j33 = j44 + 1 To m2 'a(33)
If b(a1(j33)) = 0 Then b(a1(j33)) = a1(j33): c(33) = a1(j33) Else GoTo 330
a(33) = a1(j33)
a(23) = Pr11 - a(33): If b(a(23)) = 0 Then b(a(23)) = a(23): c(23) = a(23) Else GoTo 230
a(22) = s1 - a(11) - a(33) - a(44) - a(55) - a(66) - a(77) - a(88) - a(99) - a(110) - a(121)
If a(22) < a1(m1) Or a(22) > a1(m2) Then GoTo 220:
If b1(a(22)) = 0 Then GoTo 220
If b(a(22)) = 0 Then b(a(22)) = a(22): c(22) = a(22) Else GoTo 220
a(12) = Pr11 - a(22): If b(a(12)) = 0 Then b(a(12)) = a(12): c(12) = a(12) Else GoTo 120
' Exclude solutions with identical numbers
GoSub 1800: If fl1 = 0 Then GoTo 5
n9 = n9 + 1
GoSub 650 'Print Result (Squares)
GoTo 2000 'Print Only First Square
5
b(c(12)) = 0: c(12) = 0
120 b(c(22)) = 0: c(22) = 0
220 b(c(23)) = 0: c(23) = 0
230 b(c(33)) = 0: c(33) = 0
330 Next j33
b(c(89)) = 0: c(89) = 0
890 b(c(99)) = 0: c(99) = 0
990 Next j99
b(c(100)) = 0: c(100) = 0
1000 b(c(110)) = 0: c(110) = 0
1100 Next j110
b(c(34)) = 0: c(34) = 0
340 b(c(44)) = 0: c(44) = 0
440 Next j44
b(c(45)) = 0: c(45) = 0
450 b(c(55)) = 0: c(55) = 0
550 Next j55
b(c(56)) = 0: c(56) = 0
560 b(c(66)) = 0: c(66) = 0
660 Next j66
b(c(67)) = 0: c(67) = 0
670 b(c(77)) = 0: c(77) = 0
770 Next j77
b(c(78)) = 0: c(78) = 0
780 b(c(88)) = 0: c(88) = 0
880 Next j88
b(c(11)) = 0: c(11) = 0
110 b(c(111)) = 0: c(111) = 0
1110
b(c(3)) = 0: c(3) = 0
30 b(c(113)) = 0: c(113) = 0
1130 Next j113
b(c(2)) = 0: c(2) = 0
20 b(c(112)) = 0: c(112) = 0
1120 Next j112
b(c(1)) = 0: c(1) = 0
10 b(c(121)) = 0: c(121) = 0
1210 Next j121
b(c(10)) = 0: c(10) = 0
100 b(c(120)) = 0: c(120) = 0
1200 Next j120
b(c(9)) = 0: c(9) = 0
90 b(c(119)) = 0: c(119) = 0
1190 Next j119
b(c(4)) = 0: c(4) = 0
40 b(c(114)) = 0: c(114) = 0
1140 Next j114
b(c(5)) = 0: c(5) = 0
50 b(c(115)) = 0: c(115) = 0
1150 Next j115
b(c(6)) = 0: c(6) = 0
60 b(c(116)) = 0: c(116) = 0
1160 Next j116
b(c(7)) = 0: c(7) = 0
70 b(c(117)) = 0: c(117) = 0
1170 Next j117
b(c(8)) = 0: c(8) = 0
80 b(c(118)) = 0: c(118) = 0
1180 Next j118
2000 Erase b1, b, c
Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s1)
y = MsgBox(t10, 0, "Routine MgcSqr11b")
End
' Print results (lines)
640 Cells(n9, 122).Select
For i1 = 1 To 121
Cells(n9, i1).Value = a(i1)
Next i1
Cells(n9, 122).Value = s1
Return
' Print results (squares)
650 n2 = n2 + 1
If n2 = 3 Then
n2 = 1: k1 = k1 + 12: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 12
End If
Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = n9
i3 = 0
For i1 = 1 To 11
For i2 = 1 To 11
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a(i3)
Next i2
Next i1
Return
' Exclude solutions with identical numbers
1800 fl1 = 1
For j1 = 1 To 121
a2 = a(j1): If a2 = 0 Then GoTo 1810
For j2 = (1 + j1) To 121
If a2 = a(j2) Then fl1 = 0: Return
Next j2
1810 Next j1
Return
End Sub