' Generates Associated Magic Squares of order 9
' Square Inlays Order 3 and 4 (Overlapping)
' Tested with Office 2007 under Windows 7
Sub Priem9k()
Dim a1(1260), b1(187141), b(187141), c(81), a(81), a9(81), s(4)
y = MsgBox("Locked", vbCritical, "Routine Priem9k")
End
n1 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
Sheets("Klad1").Select
t1 = Timer
For j100 = 3 To 17
Cells(k1, 1).Select: Cells(k1, 1).Value = j100
Cntr9 = Sheets("Sqrs3").Cells(j100, 6).Value
Rcrd9 = Sheets("Sqrs3").Cells(j100, 8).Value
s9 = 9 * Cntr9: Pr9 = 2 * Cntr9
i20 = Sheets("Sqrs3").Cells(j100, 3).Value
s(2) = Sheets("Sqrs3").Cells(j100, 1).Value
For i1 = 1 To 9
a(i1) = Sheets("Lines3").Cells(i20, i1).Value
Next i1
i10 = 2: GoSub 1750
i30 = Sheets("Sqrs3").Cells(j100, 4).Value
s(3) = Sheets("Sqrs3").Cells(j100, 2).Value
For i1 = 1 To 9
a(i1) = Sheets("Lines3").Cells(i30, i1).Value
Next i1
i10 = 3: GoSub 1750
' Block Used Primes
For i1 = 1 To 81
b(a9(i1)) = a9(i1)
Next i1
nVar1 = Sheets("Pairs7").Cells(Rcrd9, 9).Value
Erase b1
For i1 = 1 To nVar1
x = Sheets("Pairs7").Cells(Rcrd9, 9 + i1).Value
b1(x) = x: a1(i1) = x
Next i1
m1 = 1: m2 = nVar1
s(4) = 2 * (s(3) - Cntr9) 'Heuristic
s(1) = 8 * s9 / 9 - s(4)
GoSub 3000: If fl1 = 0 Then GoTo 1000 'Select Squares (4 x 4)
GoSub 2000: If fl1 = 0 Then GoTo 1000 'Select Border
GoSub 850: If fl1 = 0 Then GoTo 1000 'Back Check Identical Numbers
n9 = n9 + 1: GoSub 1650
1000 Erase b, c, a9
Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Priem9k")
End
' Center squares (4 x 4)
3000 fl1 = 0
t11 = Timer 'Time Out
For j42 = m1 To m2 'a9(42)
If b(a1(j42)) = 0 Then b(a1(j42)) = a1(j42): c(42) = a1(j42) Else GoTo 420
a9(42) = a1(j42)
a9(40) = Pr9 - a9(42): If b(a9(40)) = 0 Then b(a9(40)) = a9(40): c(40) = a9(40) Else GoTo 400
For j43 = m1 To m2 'a9(43)
If b(a1(j43)) = 0 Then b(a1(j43)) = a1(j43): c(43) = a1(j43) Else GoTo 430
a9(43) = a1(j43)
a9(39) = Pr9 - a9(43): If b(a9(39)) = 0 Then b(a9(39)) = a9(39): c(39) = a9(39) Else GoTo 390
a9(44) = s(4) - a9(43) - a9(42) - a9(41)
If a9(44) < a1(m1) Or a9(44) > a1(m2) Then GoTo 440
If b1(a9(44)) = 0 Then GoTo 440
If b(a9(44)) = 0 Then b(a9(44)) = a9(44): c(44) = a9(44) Else GoTo 440
a9(38) = Pr9 - a9(44): If b(a9(38)) = 0 Then b(a9(38)) = a9(38): c(38) = a9(38) Else GoTo 380
t12 = Timer: t13 = t12 - t11 'Time Out
If t13 > 10 Then Erase b, c: Return 'Time Out, Try Next
For j50 = m1 To m2 'a9(50)
If b(a1(j50)) = 0 Then b(a1(j50)) = a1(j50): c(50) = a1(j50) Else GoTo 500
a9(50) = a1(j50)
a9(32) = Pr9 - a9(50): If b(a9(32)) = 0 Then b(a9(32)) = a9(32): c(32) = a9(32) Else GoTo 320
For j51 = m1 To m2 'a9(51)
If b(a1(j51)) = 0 Then b(a1(j51)) = a1(j51): c(51) = a1(j51) Else GoTo 552
a9(51) = a1(j51)
a9(31) = Pr9 - a9(51): If b(a9(31)) = 0 Then b(a9(31)) = a9(31): c(31) = a9(31) Else GoTo 310
For j52 = m1 To m2 'a9(52)
If b(a1(j52)) = 0 Then b(a1(j52)) = a1(j52): c(52) = a1(j52) Else GoTo 520
a9(52) = a1(j52)
a9(30) = Pr9 - a9(52): If b(a9(30)) = 0 Then b(a9(30)) = a9(30): c(30) = a9(30) Else GoTo 300
a9(53) = s(4) - a9(52) - a9(51) - a9(50)
If a9(53) < a1(m1) Or a9(53) > a1(m2) Then GoTo 530
If b1(a9(53)) = 0 Then GoTo 530
If b(a9(53)) = 0 Then b(a9(53)) = a9(53): c(53) = a9(53) Else GoTo 530
a9(29) = Pr9 - a9(53): If b(a9(29)) = 0 Then b(a9(29)) = a9(29): c(29) = a9(29) Else GoTo 290
For j59 = m1 To m2 'a9(59)
If b(a1(j59)) = 0 Then b(a1(j59)) = a1(j59): c(59) = a1(j59) Else GoTo 590
a9(59) = a1(j59)
a9(23) = Pr9 - a9(59): If b(a9(23)) = 0 Then b(a9(23)) = a9(23): c(23) = a9(23) Else GoTo 230
a9(60) = a9(59) - a9(52) + a9(50) - a9(44) + a9(41)
If a9(60) < a1(m1) Or a9(60) > a1(m2) Then GoTo 600:
If b1(a9(60)) = 0 Then GoTo 600
If b(a9(60)) = 0 Then b(a9(60)) = a9(60): c(60) = a9(60) Else GoTo 600
a9(22) = Pr9 - a9(60): If b(a9(22)) = 0 Then b(a9(22)) = a9(22): c(22) = a9(22) Else GoTo 220
a9(61) = s(4) - a9(59) - a9(51) - a9(50) + a9(44) - a9(41)
If a9(61) < a1(m1) Or a9(61) > a1(m2) Then GoTo 610:
If b1(a9(61)) = 0 Then GoTo 610
If b(a9(61)) = 0 Then b(a9(61)) = a9(61): c(61) = a9(61) Else GoTo 610
a9(21) = Pr9 - a9(61): If b(a9(21)) = 0 Then b(a9(21)) = a9(21): c(21) = a9(21) Else GoTo 210
a9(62) = -a9(59) + a9(52) + a9(51)
If a9(62) < a1(m1) Or a9(62) > a1(m2) Then GoTo 620:
If b1(a9(62)) = 0 Then GoTo 620
If b(a9(62)) = 0 Then b(a9(62)) = a9(62): c(62) = a9(62) Else GoTo 620
a9(20) = Pr9 - a9(62): If b(a9(20)) = 0 Then b(a9(20)) = a9(20): c(20) = a9(20) Else GoTo 200
a9(68) = s(4) - a9(60) - a9(52) - a9(44)
If a9(68) < a1(m1) Or a9(68) > a1(m2) Then GoTo 680:
If b1(a9(68)) = 0 Then GoTo 680
If b(a9(68)) = 0 Then b(a9(68)) = a9(68): c(68) = a9(68) Else GoTo 680
a9(14) = Pr9 - a9(68): If b(a9(14)) = 0 Then b(a9(14)) = a9(14): c(14) = a9(14) Else GoTo 140
a9(69) = -s(4) - a9(59) + a9(53) + 2 * a9(52) + 2 * a9(44) + a9(43)
If a9(69) < a1(m1) Or a9(69) > a1(m2) Then GoTo 690:
If b1(a9(69)) = 0 Then GoTo 690
If b(a9(69)) = 0 Then b(a9(69)) = a9(69): c(69) = a9(69) Else GoTo 690
a9(13) = Pr9 - a9(69): If b(a9(13)) = 0 Then b(a9(13)) = a9(13): c(13) = a9(13) Else GoTo 130
a9(70) = a9(59) - a9(53) - 2 * a9(52) + a9(42) + 2 * a9(41)
If a9(70) < a1(m1) Or a9(70) > a1(m2) Then GoTo 700:
If b1(a9(70)) = 0 Then GoTo 700
If b(a9(70)) = 0 Then b(a9(70)) = a9(70): c(70) = a9(70) Else GoTo 700
a9(12) = Pr9 - a9(70): If b(a9(12)) = 0 Then b(a9(12)) = a9(12): c(12) = a9(12) Else GoTo 120
a9(71) = a9(59) + a9(50) - a9(44)
If a9(71) < a1(m1) Or a9(71) > a1(m2) Then GoTo 710:
If b1(a9(71)) = 0 Then GoTo 710
If b(a9(71)) = 0 Then b(a9(71)) = a9(71): c(71) = a9(71) Else GoTo 710
a9(11) = Pr9 - a9(71): If b(a9(11)) = 0 Then b(a9(11)) = a9(11): c(11) = a9(11) Else GoTo 110
fl1 = 1: Return
b(c(11)) = 0: c(11) = 0
110 b(c(71)) = 0: c(71) = 0
710 b(c(12)) = 0: c(12) = 0
120 b(c(70)) = 0: c(70) = 0
700 b(c(13)) = 0: c(13) = 0
130 b(c(69)) = 0: c(69) = 0
690 b(c(14)) = 0: c(14) = 0
140 b(c(68)) = 0: c(68) = 0
680 b(c(20)) = 0: c(20) = 0
200 b(c(62)) = 0: c(62) = 0
620 b(c(21)) = 0: c(21) = 0
210 b(c(61)) = 0: c(61) = 0
610 b(c(22)) = 0: c(22) = 0
220 b(c(60)) = 0: c(60) = 0
600 b(c(23)) = 0: c(23) = 0
230 b(c(59)) = 0: c(59) = 0
590 Next j59
b(c(29)) = 0: c(29) = 0
290 b(c(53)) = 0: c(53) = 0
530 b(c(30)) = 0: c(30) = 0
300 b(c(52)) = 0: c(52) = 0
520 Next j52
b(c(31)) = 0: c(31) = 0
310 b(c(51)) = 0: c(51) = 0
552 Next j51
b(c(32)) = 0: c(32) = 0
320 b(c(50)) = 0: c(50) = 0
500 Next j50
b(c(38)) = 0: c(38) = 0
380 b(c(44)) = 0: c(44) = 0
440 b(c(39)) = 0: c(39) = 0
390 b(c(43)) = 0: c(43) = 0
430 Next j43
b(c(40)) = 0: c(40) = 0
400 b(c(42)) = 0: c(42) = 0
420 Next j42
Return
' Select Border
2000 fl1 = 0
t11 = Timer 'Time Out
For j81 = m1 To m2 'a9(81)
If b(a1(j81)) = 0 Then b(a1(j81)) = a1(j81): c(81) = a1(j81) Else GoTo 810
a9(81) = a1(j81)
a9(1) = Pr9 - a9(81): If b(a9(1)) = 0 Then b(a9(1)) = a9(1): c(1) = a9(1) Else GoTo 10
For j80 = m1 To m2 'a9(80)
If b(a1(j80)) = 0 Then b(a1(j80)) = a1(j80): c(80) = a1(j80) Else GoTo 800
a9(80) = a1(j80)
a9(74) = -s9 / 9 + a9(80) - s(3) + s(4)
If a9(74) < a1(m1) Or a9(74) > a1(m2) Then GoTo 740
If b1(a9(74)) = 0 Then GoTo 740
If b(a9(74)) = 0 Then b(a9(74)) = a9(74): c(74) = a9(74) Else GoTo 740
a9(8) = Pr9 - a9(74): If b(a9(8)) = 0 Then b(a9(8)) = a9(8): c(8) = a9(8) Else GoTo 80
a9(2) = Pr9 - a9(80): If b(a9(2)) = 0 Then b(a9(2)) = a9(2): c(2) = a9(2) Else GoTo 20
t12 = Timer: t13 = t12 - t11 'Time Out
If t13 > 10 Then Erase b, c: Return 'Time Out, Try Next
For j79 = m1 To m2 'a9(79)
If b(a1(j79)) = 0 Then b(a1(j79)) = a1(j79): c(79) = a1(j79) Else GoTo 790
a9(79) = a1(j79)
a9(75) = -s9 / 9 + a9(79) - s(3) + s(4)
If a9(75) < a1(m1) Or a9(75) > a1(m2) Then GoTo 750
If b1(a9(75)) = 0 Then GoTo 750
If b(a9(75)) = 0 Then b(a9(75)) = a9(75): c(75) = a9(75) Else GoTo 750
a9(7) = Pr9 - a9(75): If b(a9(7)) = 0 Then b(a9(7)) = a9(7): c(7) = a9(7) Else GoTo 70
a9(3) = Pr9 - a9(79): If b(a9(3)) = 0 Then b(a9(3)) = a9(3): c(3) = a9(3) Else GoTo 30
For j78 = m1 To m2 'a9(78)
If b(a1(j78)) = 0 Then b(a1(j78)) = a1(j78): c(78) = a1(j78) Else GoTo 780
a9(78) = a1(j78)
a9(76) = -s9 / 9 + a9(78) - s(3) + s(4)
If a9(76) < a1(m1) Or a9(76) > a1(m2) Then GoTo 760
If b1(a9(76)) = 0 Then GoTo 760
If b(a9(76)) = 0 Then b(a9(76)) = a9(76): c(76) = a9(76) Else GoTo 760
a9(6) = Pr9 - a9(76): If b(a9(6)) = 0 Then b(a9(6)) = a9(6): c(6) = a9(6) Else GoTo 60
a9(4) = Pr9 - a9(78): If b(a9(4)) = 0 Then b(a9(4)) = a9(4): c(4) = a9(4) Else GoTo 40
For j77 = m1 To m2 'a9(77)
If b(a1(j77)) = 0 Then b(a1(j77)) = a1(j77): c(77) = a1(j77) Else GoTo 770
a9(77) = a1(j77)
a9(73) = 12 * s9 / 9 - a9(77) - 2 * a9(78) - 2 * a9(79) - 2 * a9(80) - a9(81) + 3 * s(3) - 3 * s(4)
If a9(73) < a1(m1) Or a9(73) > a1(m2) Then GoTo 730
If b1(a9(73)) = 0 Then GoTo 730
If b(a9(73)) = 0 Then b(a9(73)) = a9(73): c(73) = a9(73) Else GoTo 730
a9(9) = Pr9 - a9(73): If b(a9(9)) = 0 Then b(a9(9)) = a9(9): c(9) = a9(9) Else GoTo 90
a9(5) = Pr9 - a9(77): If b(a9(5)) = 0 Then b(a9(5)) = a9(5): c(5) = a9(5) Else GoTo 50
For j72 = m1 To m2 'a9(72)
If b(a1(j72)) = 0 Then b(a1(j72)) = a1(j72): c(72) = a1(j72) Else GoTo 720
a9(72) = a1(j72)
a9(64) = s9 - a9(72) - s(3) - s(4)
If a9(64) < a1(m1) Or a9(64) > a1(m2) Then GoTo 640
If b1(a9(64)) = 0 Then GoTo 640
If b(a9(64)) = 0 Then b(a9(64)) = a9(64): c(64) = a9(64) Else GoTo 640
a9(18) = Pr9 - a9(64): If b(a9(18)) = 0 Then b(a9(18)) = a9(18): c(18) = a9(18) Else GoTo 180
a9(10) = Pr9 - a9(72): If b(a9(10)) = 0 Then b(a9(10)) = a9(10): c(10) = a9(10) Else GoTo 100
For j63 = m1 To m2 'a9(63)
If b(a1(j63)) = 0 Then b(a1(j63)) = a1(j63): c(63) = a1(j63) Else GoTo 630
a9(63) = a1(j63)
a9(55) = s9 - a9(63) - s(3) - s(4)
If a9(55) < a1(m1) Or a9(55) > a1(m2) Then GoTo 550
If b1(a9(55)) = 0 Then GoTo 550
If b(a9(55)) = 0 Then b(a9(55)) = a9(55): c(55) = a9(55) Else GoTo 550
a9(27) = Pr9 - a9(55): If b(a9(27)) = 0 Then b(a9(27)) = a9(27): c(27) = a9(27) Else GoTo 270
a9(19) = Pr9 - a9(63): If b(a9(19)) = 0 Then b(a9(19)) = a9(19): c(19) = a9(19) Else GoTo 190
For j54 = m1 To m2 'a9(54)
If b(a1(j54)) = 0 Then b(a1(j54)) = a1(j54): c(54) = a1(j54) Else GoTo 540
a9(54) = a1(j54)
a9(46) = s9 - a9(54) - s(3) - s(4)
If a9(46) < a1(m1) Or a9(46) > a1(m2) Then GoTo 460
If b1(a9(46)) = 0 Then GoTo 460
If b(a9(46)) = 0 Then b(a9(46)) = a9(46): c(46) = a9(46) Else GoTo 460
a9(45) = 40 * s9 / 9 - 2*a9(54) - 2*a9(63) - 2*a9(72) - a9(77) - 2*a9(78) - 2*a9(79) - 2*a9(80) - 2*a9(81) - 6*s(4)
If a9(45) < a1(m1) Or a9(45) > a1(m2) Then GoTo 450
If b1(a9(45)) = 0 Then GoTo 450
If b(a9(45)) = 0 Then b(a9(45)) = a9(45): c(45) = a9(45) Else GoTo 450
a9(37) = Pr9 - a9(45): If b(a9(37)) = 0 Then b(a9(37)) = a9(37): c(37) = a9(37) Else GoTo 370
a9(36) = Pr9 - a9(46): If b(a9(36)) = 0 Then b(a9(36)) = a9(36): c(36) = a9(36) Else GoTo 360
a9(28) = Pr9 - a9(54): If b(a9(28)) = 0 Then b(a9(28)) = a9(28): c(28) = a9(28) Else GoTo 280
fl1 = 1: Return
b(c(28)) = 0: c(28) = 0
280 b(c(36)) = 0: c(36) = 0
360 b(c(37)) = 0: c(37) = 0
370 b(c(45)) = 0: c(45) = 0
450 b(c(46)) = 0: c(46) = 0
460 b(c(54)) = 0: c(54) = 0
540 Next j54
b(c(19)) = 0: c(19) = 0
190 b(c(27)) = 0: c(27) = 0
270 b(c(55)) = 0: c(55) = 0
550 b(c(63)) = 0: c(63) = 0
630 Next j63
b(c(10)) = 0: c(10) = 0
100 b(c(18)) = 0: c(18) = 0
180 b(c(64)) = 0: c(64) = 0
640 b(c(72)) = 0: c(72) = 0
720 Next j72
b(c(5)) = 0: c(5) = 0
50 b(c(9)) = 0: c(9) = 0
90 b(c(73)) = 0: c(73) = 0
730 b(c(77)) = 0: c(77) = 0
770 Next j77
b(c(4)) = 0: c(4) = 0
40 b(c(6)) = 0: c(6) = 0
60 b(c(76)) = 0: c(76) = 0
760 b(c(78)) = 0: c(78) = 0
780 Next j78
b(c(3)) = 0: c(3) = 0
30 b(c(7)) = 0: c(7) = 0
70 b(c(75)) = 0: c(75) = 0
750 b(c(79)) = 0: c(79) = 0
790 Next j79
b(c(2)) = 0: c(2) = 0
20 b(c(8)) = 0: c(8) = 0
80 b(c(74)) = 0: c(74) = 0
740 b(c(80)) = 0: c(80) = 0
800 Next j80
b(c(1)) = 0: c(1) = 0
10 b(c(81)) = 0: c(81) = 0
810 Next j81
Return
' Assign Sub Squares
1750 Select Case i10
Case 2 'Top / Right
a9(15) = a(1): a9(16) = a(2): a9(17) = a(3):
a9(24) = a(4): a9(25) = a(5): a9(26) = a(6):
a9(33) = a(7): a9(34) = a(8): a9(35) = a(9):
a9(41) = Cntr9
Case 3 'Bottom / Left
a9(47) = a(1): a9(48) = a(2): a9(49) = a(3):
a9(56) = a(4): a9(57) = a(5): a9(58) = a(6):
a9(65) = a(7): a9(66) = a(8): a9(67) = a(9):
End Select
Return
' Exclude solutions with identical numbers a8()
850 fl1 = 1
For j1 = 1 To 81
a20 = a9(j1): If a20 = 0 Then GoTo 860
For j2 = (1 + j1) To 81
If a20 = a9(j2) Then fl1 = 0: Return
Next j2
860 Next j1
Return
' Print results (squares)
1650 n2 = n2 + 1
If n2 = 3 Then
n2 = 1: k1 = k1 + 10: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 10
End If
Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = "MC = " + CStr(s9)
i3 = 0
For i1 = 1 To 9
For i2 = 1 To 9
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a9(i3)
Next i2
Next i1
Return
End Sub