' Generates Crosswise Symmetric (Semi) Latin (Diagonal) Squares of order 5
' Tested with Office 365 under Windows 10
Sub SemiLat5d()
Dim a(25), a1(5), b(5)
y = MsgBox("Locked", vbExclamation, "Routine SemiLat5d")
End
n1 = 0: n9 = 0: k1 = 1: k2 = 1
m1 = 1: m2 = 5: s1 = 10: Pr5 = 2 * s1 / 5
a1(1) = 0: a1(2) = 1: a1(3) = 2: a1(4) = 3: a1(5) = 4
Sheets("Klad1").Select
t1 = Timer
a(13) = 2
For j25 = m1 To m2 'a(25)
a(25) = a1(j25)
For j24 = m1 To m2 'a(24)
a(24) = a1(j24)
If a(24) = a(25) Then GoTo 240
For j23 = m1 To m2 'a(23)
a(23) = a1(j23)
If a(23) = a(24) Or a(23) = a(25) Then GoTo 230
For j22 = m1 To m2 'a(22)
a(22) = a1(j22)
If a(22) = a(23) Or a(22) = a(24) Or a(22) = a(25) Then GoTo 220
a(21) = s1 - a(22) - a(23) - a(24) - a(25)
If a(21) < a1(m1) Or a(21) > a1(m2) Then GoTo 220
a(19) = Pr5 - a(25): a(20) = Pr5 - a(24): a(18) = Pr5 - a(23): a(16) = Pr5 - a(22): a(17) = Pr5 - a(21):
For j15 = m1 To m2 'a(15)
a(15) = a1(j15)
a(14) = Pr5 - a(15):
For j12 = m1 To m2 'a(12)
a(12) = a1(j12)
a(11) = Pr5 - a(12):
For j10 = m1 To m2 'a(10)
a(10) = a1(j10)
a(4) = Pr5 - a(10):
a(9) = -s1 / 5 + a(10) + a(15) - a(24) + a(25)
If a(9) < a1(m1) Or a(9) > a1(m2) Then GoTo 100
a(5) = Pr5 - a(9):
For j8 = m1 To m2 'a(8)
a(8) = a1(j8)
a(3) = Pr5 - a(8):
a(7) = (7 * s1 / 5 - a(8) - 2 * a(10) - a(12) - a(15) + a(21) - a(22) + a(24) - a(25)) / 2
If a(7) < a1(m1) Or a(7) > a1(m2) Or CInt(a(7)) <> a(7) Then GoTo 80
a(1) = Pr5 - a(7):
a(6) = s1 - a(7) - a(8) - a(9) - a(10)
If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 80
a(2) = Pr5 - a(6):
' Check Latin Lines
GoSub 400: If fl1 = 0 Then GoTo 80 'Latin Rows
' GoSub 410: If fl1 = 0 Then GoTo 80 'Latin Columns (option)
' GoSub 420: If fl1 = 0 Then GoTo 80 'Latin Main Diagonals (option)
' n9 = n9 + 1: Cells(1, 1) = n9 'Counting
n9 = n9 + 1: GoSub 640 'Print results (selected numbers)
' n9 = n9 + 1: GoSub 650 'Print results (squares)
80 Next j8
100 Next j10
120 Next j12
150 Next j15
220 Next j22
230 Next j23
240 Next j24
250 Next j25
t2 = Timer
t10 = Str(t2 - t1) + " sec, " + Str(n9) + " Solutions"
y = MsgBox(t10, vbInformation, "Routine SemiLat5d")
End
' Check Latin Rows
400 fl1 = 1
b(1) = a(1): b(2) = a(2): b(3) = a(3): b(4) = a(4): b(5) = a(5):
GoSub 300: If fl1 = 0 Then GoTo 80
b(1) = a(6): b(2) = a(7): b(3) = a(8): b(4) = a(9): b(5) = a(10):
GoSub 300: If fl1 = 0 Then GoTo 80
b(1) = a(11): b(2) = a(12): b(3) = a(13): b(4) = a(14): b(5) = a(15):
GoSub 300: If fl1 = 0 Then GoTo 80
b(1) = a(16): b(2) = a(17): b(3) = a(18): b(4) = a(19): b(5) = a(20):
GoSub 300: If fl1 = 0 Then GoTo 80
b(1) = a(21): b(2) = a(22): b(3) = a(23): b(4) = a(24): b(5) = a(25):
GoSub 300: If fl1 = 0 Then GoTo 80
Return
' Check Latin Columns
410 fl1 = 1
b(1) = a(5): b(2) = a(10): b(3) = a(15): b(4) = a(20): b(5) = a(25):
GoSub 300: If fl1 = 0 Then GoTo 80
b(1) = a(4): b(2) = a(9): b(3) = a(14): b(4) = a(19): b(5) = a(24):
GoSub 300: If fl1 = 0 Then GoTo 80
b(1) = a(3): b(2) = a(8): b(3) = a(13): b(4) = a(18): b(5) = a(23):
GoSub 300: If fl1 = 0 Then GoTo 80
b(1) = a(2): b(2) = a(7): b(3) = a(12): b(4) = a(17): b(5) = a(22):
GoSub 300: If fl1 = 0 Then GoTo 80
b(1) = a(1): b(2) = a(6): b(3) = a(11): b(4) = a(16): b(5) = a(21):
GoSub 300: If fl1 = 0 Then GoTo 80
Return
' Check Latin Diagonals
420 fl1 = 1
b(1) = a(1): b(2) = a(7): b(3) = a(13): b(4) = a(19): b(5) = a(25):
GoSub 300: If fl1 = 0 Then Return
b(1) = a(5): b(2) = a(9): b(3) = a(13): b(4) = a(17): b(5) = a(21):
GoSub 300: If fl1 = 0 Then Return
Return
300 fl1 = 1
For i1 = 1 To 5
b2 = b(i1)
For i2 = (1 + i1) To 5
If b2 = b(i2) Then fl1 = 0: Return
Next i2
Next i1
Return
' Print results (selected numbers)
640 ''Cells(n9, 26).Select
For i1 = 1 To 25
Cells(n9, i1).Value = a(i1)
Next i1
Cells(n9, 26).Value = n9
Cells(1, 27).Value = n9
Return
' Print results (squares)
650 n1 = n1 + 1
If n1 = 5 Then
n1 = 1: k1 = k1 + 6: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 6
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 5
For i2 = 1 To 5
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a(i3)
Next i2
Next i1
Return
End Sub