' Generates Auxiliary Squares of order 3, Non Consecutive Integers
' Construction Order 13 Quadrant P14 Bordered Magic Squares
' Tested with Office 365 under Windows 10
Sub Priem3c()
Dim a1(48), a(9), b1(169), b(169), c(9)
y = MsgBox("Locked", vbCritical, "Routine Priem3c")
End
n1 = 0: n9 = 0: k1 = 1: k2 = 1
m1 = 1: m2 = 48
' Define Intgers, Border 13 x 13 (No Solutions)
'' a1(1) = 1: a1(2) = 2: a1(3) = 3: a1(4) = 4: a1(5) = 5: a1(6) = 6: a1(7) = 7: a1(8) = 8:
'' a1(9) = 9: a1(10) = 10: a1(11) = 11: a1(12) = 12: a1(13) = 13: a1(14) = 14: a1(15) = 15: a1(16) = 16:
'' a1(17) = 17: a1(18) = 18: a1(19) = 19: a1(20) = 20: a1(21) = 21: a1(22) = 22: a1(23) = 23: a1(24) = 24:
'' a1(25) = 146: a1(26) = 147: a1(27) = 148: a1(28) = 149: a1(29) = 150: a1(30) = 151: a1(31) = 152: a1(32) = 153:
'' a1(33) = 154: a1(34) = 155: a1(35) = 156: a1(36) = 157: a1(37) = 158: a1(38) = 159: a1(39) = 160: a1(40) = 161:
'' a1(41) = 162: a1(42) = 163: a1(43) = 164: a1(44) = 165: a1(45) = 166: a1(46) = 167: a1(47) = 168: a1(48) = 169:
' Semi Latin Square Based
a1(1) = 1: a1(2) = 2: a1(3) = 3: a1(4) = 4: a1(5) = 5: a1(6) = 6: a1(7) = 7: a1(8) = 8:
a1(9) = 9: a1(10) = 10: a1(11) = 11: a1(12) = 12: a1(13) = 13: a1(14) = 14: a1(15) = 26: a1(16) = 27:
a1(17) = 39: a1(18) = 40: a1(19) = 52: a1(20) = 53: a1(21) = 65: a1(22) = 66: a1(23) = 78: a1(24) = 79:
a1(25) = 91: a1(26) = 92: a1(27) = 104: a1(28) = 105: a1(29) = 117: a1(30) = 118: a1(31) = 130: a1(32) = 131:
a1(33) = 143: a1(34) = 144: a1(35) = 156: a1(36) = 157: a1(37) = 158: a1(38) = 159: a1(39) = 160: a1(40) = 161:
a1(41) = 162: a1(42) = 163: a1(43) = 164: a1(44) = 165: a1(45) = 166: a1(46) = 167: a1(47) = 168: a1(48) = 169:
For i1 = m1 To m2
b1(a1(i1)) = a1(i1)
Next i1
' Generate Squares
Sheets("Klad1").Select
t1 = Timer
a(5) = 85: b(a(5)) = a(5)
For j9 = m1 To m2 'a(9)
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 b(a1(j8)) = 0 Then b(a1(j8)) = a1(j8): c(8) = a1(j8) Else GoTo 120
a(8) = a1(j8)
a(7) = 340 - 2 * a(8) - a(9)
If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 110:
If a(7) < a(9) Then GoTo 110
If b1(a(7)) = 0 Then GoTo 110
a(6) = 255 - a(8) - a(9)
If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 110:
If b1(a(6)) = 0 Then GoTo 110
a(4) = -85 + a(8) + 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) = -170 + 2 * a(8) + a(9)
If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 110:
If a(3) < a(9) Then GoTo 110
If a(3) < a(7) Then GoTo 110
If b1(a(3)) = 0 Then GoTo 110
a(2) = 170 - 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) = 170 - a(9)
If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 110:
If a(1) < a(9) Then GoTo 110
If b1(a(1)) = 0 Then GoTo 110
' Exclude solutions with identical numbers
GoSub 800: If fl1 = 0 Then GoTo 110
' n9 = n9 + 1: GoSub 640 'Print results (selected numbers)
n9 = n9 + 1: GoSub 650 'Print results (squares)
110 b(c(8)) = 0: c(8) = 0
120 Next j8
b(c(9)) = 0: c(9) = 0
160 Next j9
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Priem3c")
End
' Print results (selected numbers)
640 Cells(n9, 9).Select
For i1 = 1 To 9
Cells(n9, i1).Value = a(i1)
Next i1
Return
' Print results (squares)
650 n1 = n1 + 1
If n1 = 5 Then
n1 = 1: k1 = k1 + 4: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 4
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 3
For i2 = 1 To 3
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a(i3)
Next i2
Next i1
Return
' Exclude solutions with identical numbers
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
End Sub