' Generates Inlaid Magic Squares of order 9
' Based on Sub Squares with Different Magic Sums
' Tested with Office 2007 under Windows 7
Sub Priem9k()
Dim a1(250), a(9), a9(81), b1(15355), b(15355), c(9)
y = MsgBox("Locked", vbCritical, "Routine Priem9k")
End
n1 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
Sheets("Klad1").Select
t1 = Timer
For j200 = 2 To 35
Erase a9
'Define Center Elements
For i1 = 1 To 9
a(i1) = Sheets("Input9").Cells(j200, i1).Value
Next i1
a9(11) = a(1): a9(14) = a(2): a9(17) = a(3):
a9(38) = a(4): a9(41) = a(5): a9(44) = a(6):
a9(65) = a(7): a9(68) = a(8): a9(71) = 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 = 3 * 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 81
b1(a9(i1)) = 0
Next i1
' Generate Squares
For j9 = m1 To m2 'a(9)
If b1(a1(j9)) = 0 Then GoTo 160
If b(a1(j9)) = 0 Then b(a1(j9)) = a1(j9): c(9) = a1(j9) Else GoTo 160
a(9) = a1(j9)
For j8 = m1 To m2 'a(8)
If b1(a1(j8)) = 0 Then GoTo 120
If b(a1(j8)) = 0 Then b(a1(j8)) = a1(j8): c(8) = a1(j8) Else GoTo 120
a(8) = a1(j8)
a(7) = s1 - a(8) - a(9):
If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 110:
If b1(a(7)) = 0 Then GoTo 110
a(6) = 4 * s1 / 3 - a(8) - 2 * a(9):
If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 110:
If b1(a(6)) = 0 Then GoTo 110
a(5) = s1 / 3:
If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 110:
''If b1(a(5)) = 0 Then GoTo 110
a(4) = -2 * s1 / 3 + 1 * a(8) + 2 * a(9):
If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 110:
If b1(a(4)) = 0 Then GoTo 110
a(3) = -s1 / 3 + 1 * a(8) + 1 * a(9):
If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 110:
If b1(a(3)) = 0 Then GoTo 110
a(2) = 2 * s1 / 3 - a(8):
If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 110:
If b1(a(2)) = 0 Then GoTo 110
a(1) = 2 * s1 / 3 - a(9):
If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 110:
If b1(a(1)) = 0 Then GoTo 110
' Exclude solutions with identical numbers
GoSub 800: If fl1 = 0 Then GoTo 110
n10 = n10 + 1: GoSub 750 'Assign to a9()
Erase b, c: GoTo 5 'Assign only first square
110 b(c(8)) = 0: c(8) = 0
120 Next j8
b(c(9)) = 0: c(9) = 0
160 Next j9
' 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 n10 = 0
Next j200
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Priem9k")
End
' Assign to a9()
750 Select Case n10
Case 1
a9(1) = a(1): a9(2) = a(2): a9(3) = a(3):
a9(10) = a(4): a9(11) = a(5): a9(12) = a(6):
a9(19) = a(7): a9(20) = a(8): a9(21) = a(9):
Case 2
a9(4) = a(1): a9(5) = a(2): a9(6) = a(3):
a9(13) = a(4): a9(14) = a(5): a9(15) = a(6):
a9(22) = a(7): a9(23) = a(8): a9(24) = a(9):
Case 3
a9(7) = a(1): a9(8) = a(2): a9(9) = a(3):
a9(16) = a(4): a9(17) = a(5): a9(18) = a(6):
a9(25) = a(7): a9(26) = a(8): a9(27) = a(9):
Case 4
a9(28) = a(1): a9(29) = a(2): a9(30) = a(3):
a9(37) = a(4): a9(38) = a(5): a9(39) = a(6):
a9(46) = a(7): a9(47) = a(8): a9(48) = a(9):
Case 5
a9(31) = a(1): a9(32) = a(2): a9(33) = a(3):
a9(40) = a(4): a9(41) = a(5): a9(42) = a(6):
a9(49) = a(7): a9(50) = a(8): a9(51) = a(9):
Case 6
a9(34) = a(1): a9(35) = a(2): a9(36) = a(3):
a9(43) = a(4): a9(44) = a(5): a9(45) = a(6):
a9(52) = a(7): a9(53) = a(8): a9(54) = a(9):
Case 7
a9(55) = a(1): a9(56) = a(2): a9(57) = a(3):
a9(64) = a(4): a9(65) = a(5): a9(66) = a(6):
a9(73) = a(7): a9(74) = a(8): a9(75) = a(9):
Case 8
a9(58) = a(1): a9(59) = a(2): a9(60) = a(3):
a9(67) = a(4): a9(68) = a(5): a9(69) = a(6):
a9(76) = a(7): a9(77) = a(8): a9(78) = a(9):
Case 9
a9(61) = a(1): a9(62) = a(2): a9(63) = a(3):
a9(70) = a(4): a9(71) = a(5): a9(72) = a(6):
a9(79) = a(7): a9(80) = a(8): a9(81) = a(9):
End Select
Return
' Print results: squares a9()
660 n1 = n1 + 1
If n1 = 2 Then
n1 = 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 = 9 * a9(41)
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
' Exclude solutions with identical numbers a()
800 fl1 = 1
For j1 = 1 To 9
a2 = a(j1)
For j2 = (1 + j1) To 9
If a2 = a(j2) Then fl1 = 0: Return
Next j2
Next j1
Return
' Exclude solutions with identical numbers a9()
850 fl1 = 1
For j1 = 1 To 81
a2 = a9(j1): If a2 = 0 Then GoTo 860
For j2 = (1 + j1) To 81
If a2 = a9(j2) Then fl1 = 0: Return
Next j2
860 Next j1
Return
End Sub