' Generates Concentric Magic Squares of order 7 for Odd Numbers
' Suitable for Concentric Lozenge Squares of order 13 (Predefined Border)
' Tested with Office 2007 under Windows 7
Sub Priem7d1()
Dim a1(81), a(25), a7(49), b(169), b1(169), c7(49)
y = MsgBox("Locked", vbCritical, "Routine Priem7d1")
End
n2 = 0: n3 = 0: n9 = 0: k1 = 1: k2 = 1
Sheets("Klad1").Select
t1 = Timer
For j100 = 2 To 365
' Define Variables (Odd Numbers)
a1(1) = 3: a1(2) = 5: a1(3) = 7: a1(4) = 9: a1(5) = 11:
a1(6) = 15: a1(7) = 17: a1(8) = 19: a1(9) = 21:
a1(10) = 23: a1(11) = 25: a1(12) = 27: a1(13) = 29: a1(14) = 31:
a1(15) = 33: a1(16) = 35: a1(17) = 37: a1(18) = 39:
a1(19) = 41: a1(20) = 43: a1(21) = 45: 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) = 75:
a1(37) = 77: a1(38) = 79: a1(39) = 81: a1(40) = 83: a1(41) = 85:
a1(42) = 87: a1(43) = 89: a1(44) = 91: a1(45) = 93:
a1(46) = 95: a1(47) = 97: a1(48) = 99: a1(49) = 101: a1(50) = 103:
a1(51) = 105: a1(52) = 107: a1(53) = 109: a1(54) = 111:
a1(55) = 113: a1(56) = 115: a1(57) = 117: a1(58) = 119: a1(59) = 121:
a1(60) = 123: a1(61) = 125: a1(62) = 127: a1(63) = 129:
a1(64) = 131: a1(65) = 133: a1(66) = 135: a1(67) = 137: a1(68) = 139:
a1(69) = 141: a1(70) = 143: a1(71) = 145: a1(72) = 147:
a1(73) = 149: a1(74) = 151: a1(75) = 153: a1(76) = 155: a1(77) = 159:
a1(78) = 161: a1(79) = 163: a1(80) = 165: a1(81) = 167:
s7 = 595: s2 = 170: m1 = 1: m2 = 81
aMin = a1(1): aMax = a1(81)
Erase b1
For i1 = 1 To 81
b1(a1(i1)) = a1(i1)
Next i1
' Read Center Square
For i1 = 1 To 25
a(i1) = Sheets("Center5").Cells(j100, i1): b1(a(i1)) = 0
Next i1
' Reduce Variables
i2 = 0
For i1 = aMin To aMax
If b1(i1) = i1 Then
i2 = i2 + 1: a1(i2) = i1
End If
Next i1
m2 = i2
Erase b1
For i1 = m1 To m2
b1(a1(i1)) = a1(i1)
Next i1
' store Center Square in a7()
Erase a7
a7(9) = a(1): a7(10) = a(2): a7(11) = a(3): a7(12) = a(4): a7(13) = a(5):
a7(16) = a(6): a7(17) = a(7): a7(18) = a(8): a7(19) = a(9): a7(20) = a(10):
a7(23) = a(11): a7(24) = a(12): a7(25) = a(13): a7(26) = a(14): a7(27) = a(15):
a7(30) = a(16): a7(31) = a(17): a7(32) = a(18): a7(33) = a(19): a7(34) = a(20):
a7(37) = a(21): a7(38) = a(22): a7(39) = a(23): a7(40) = a(24): a7(41) = a(25):
' Complete Border(s)
For j49 = m1 To m2 'a7(49) odd
If b(a1(j49)) = 0 Then b(a1(j49)) = a1(j49): c7(49) = a1(j49) Else GoTo 2490
a7(49) = a1(j49)
a7(1) = s2 - a7(49): If b(a7(1)) = 0 Then b(a7(1)) = a7(1): c7(1) = a7(1) Else GoTo 2010
For j48 = m1 To m2 'a7(48) odd
If b(a1(j48)) = 0 Then b(a1(j48)) = a1(j48): c7(48) = a1(j48) Else GoTo 2480
a7(48) = a1(j48)
a7(6) = s2 - a7(48): If b(a7(6)) = 0 Then b(a7(6)) = a7(6): c7(6) = a7(6) Else GoTo 2060
For j47 = m1 To m2 'a7(47) odd
If b(a1(j47)) = 0 Then b(a1(j47)) = a1(j47): c7(47) = a1(j47) Else GoTo 2470
a7(47) = a1(j47)
a7(5) = s2 - a7(47): If b(a7(5)) = 0 Then b(a7(5)) = a7(5): c7(5) = a7(5) Else GoTo 2050
For j46 = m1 To m2 'a7(46) odd
If b(a1(j46)) = 0 Then b(a1(j46)) = a1(j46): c7(46) = a1(j46) Else GoTo 2460
a7(46) = a1(j46)
a7(4) = s2 - a7(46): If b(a7(4)) = 0 Then b(a7(4)) = a7(4): c7(4) = a7(4) Else GoTo 2040
For j45 = m1 To m2 'a7(45) odd
If b(a1(j45)) = 0 Then b(a1(j45)) = a1(j45): c7(45) = a1(j45) Else GoTo 2450
a7(45) = a1(j45)
a7(3) = s2 - a7(45): If b(a7(3)) = 0 Then b(a7(3)) = a7(3): c7(3) = a7(3) Else GoTo 2030
For j44 = m1 To m2 'a7(44) odd
If b(a1(j44)) = 0 Then b(a1(j44)) = a1(j44): c7(44) = a1(j44) Else GoTo 2440
a7(44) = a1(j44)
a7(43) = s7 - a7(44) - a7(45) - a7(46) - a7(47) - a7(48) - a7(49)
If a7(43) < a1(m1) Or a7(43) > a1(m2) Then GoTo 2430
If b1(a7(43)) = 0 Then GoTo 2430
If b(a7(43)) = 0 Then b(a7(43)) = a7(43): c7(43) = a7(43) Else GoTo 2430
a7(7) = s2 - a7(43): If b(a7(7)) = 0 Then b(a7(7)) = a7(7): c7(7) = a7(7) Else GoTo 2070
a7(2) = s2 - a7(44): If b(a7(2)) = 0 Then b(a7(2)) = a7(2): c7(2) = a7(2) Else GoTo 2020
For j42 = m1 To m2 'a7(42) odd
If b(a1(j42)) = 0 Then b(a1(j42)) = a1(j42): c7(42) = a1(j42) Else GoTo 2420
a7(42) = a1(j42)
a7(36) = s2 - a7(42): If b(a7(36)) = 0 Then b(a7(36)) = a7(36): c7(36) = a7(36) Else GoTo 2360
For j35 = m1 To m2 'a7(35) odd
If b(a1(j35)) = 0 Then b(a1(j35)) = a1(j35): c7(35) = a1(j35) Else GoTo 2350
a7(35) = a1(j35)
a7(29) = s2 - a7(35): If b(a7(29)) = 0 Then b(a7(29)) = a7(29): c7(29) = a7(29) Else GoTo 2290
For j28 = m1 To m2 'a7(28) odd
If b(a1(j28)) = 0 Then b(a1(j28)) = a1(j28): c7(28) = a1(j28) Else GoTo 2280
a7(28) = a1(j28)
a7(22) = s2 - a7(28): If b(a7(22)) = 0 Then b(a7(22)) = a7(22): c7(22) = a7(22) Else GoTo 2220
For j21 = m1 To m2 'a7(21) odd
If b(a1(j21)) = 0 Then b(a1(j21)) = a1(j21): c7(21) = a1(j21) Else GoTo 2210
a7(21) = a1(j21)
a7(15) = s2 - a7(21): If b(a7(15)) = 0 Then b(a7(15)) = a7(15): c7(15) = a7(15) Else GoTo 2150
a7(14) = -3 * s2 / 2 + a7(15) + a7(22) + a7(29) + a7(36) + a7(43) - a7(49)
If a7(14) < a1(m1) Or a7(14) > a1(m2) Then GoTo 2140
If b1(a7(14)) = 0 Then GoTo 2140
If b(a7(14)) = 0 Then b(a7(14)) = a7(14): c7(14) = a7(14) Else GoTo 2140
a7(8) = s2 - a7(14): If b(a7(8)) = 0 Then b(a7(8)) = a7(8): c7(8) = a7(8) Else GoTo 2080
GoSub 800: If fl1 = 0 Then GoTo 5 '(Back Check)
n9 = n9 + 1: GoSub 640 'Print results (selected numbers)
' n9 = n9 + 1: GoSub 660 'Print results (squares)
Erase b, c7: GoTo 500 'Print only first square
5
b(c7(8)) = 0: c7(8) = 0
2080 b(c7(14)) = 0: c7(14) = 0
2140 b(c7(15)) = 0: c7(15) = 0
2150 b(c7(21)) = 0: c7(21) = 0
2210 Next j21
b(c7(22)) = 0: c7(22) = 0
2220 b(c7(28)) = 0: c7(28) = 0
2280 Next j28
b(c7(29)) = 0: c7(29) = 0
2290 b(c7(35)) = 0: c7(35) = 0
2350 Next j35
b(c7(36)) = 0: c7(36) = 0
2360 b(c7(42)) = 0: c7(42) = 0
2420 Next j42
b(c7(2)) = 0: c7(2) = 0
2020 b(c7(7)) = 0: c7(7) = 0
2070 b(c7(43)) = 0: c7(43) = 0
2430 b(c7(44)) = 0: c7(44) = 0
2440 Next j44
b(c7(3)) = 0: c7(3) = 0
2030 b(c7(45)) = 0: c7(45) = 0
2450 Next j45
b(c7(4)) = 0: c7(4) = 0
2040 b(c7(46)) = 0: c7(46) = 0
2460 Next j46
b(c7(5)) = 0: c7(5) = 0
2050 b(c7(47)) = 0: c7(47) = 0
2470 Next j47
b(c7(6)) = 0: c7(6) = 0
2060 b(c7(48)) = 0: c7(48) = 0
2480 Next j48
b(c7(1)) = 0: c7(1) = 0
2010 b(c7(49)) = 0: c7(49) = 0
2490 Next j49
Erase b, c7
500 Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s7)
y = MsgBox(t10, 0, "Routine Priem7d")
End
' Print results (selected numbers)
640 For i1 = 1 To 49
Cells(n9, i1).Value = a7(i1)
Next i1
Cells(n9, 50).Value = n9
Cells(n9, 50).Select
Return
' Print Square a7()
660 n2 = n2 + 1
If n2 = 5 Then
n2 = 1: k1 = k1 + 8: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 8
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 7
For i2 = 1 To 7
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a7(i3)
Next i2
Next i1
Return
' Exclude solutions with identical numbers
800 fl1 = 1
For j1 = 1 To 49
a2 = a7(j1): If a2 = 0 Then GoTo 810
For j2 = (1 + j1) To 49
If a2 = a7(j2) Then fl1 = 0: Return
Next j2
810 Next j1
Return
End Sub