' Generates Latin Squares of order 3
' Tested with Office 2007 under Windows 7
Sub LatSqr3()
Dim a(9), a1(3)
y = MsgBox("Locked", vbExclamation, "Routine LatSqr3")
End
n1 = 0: n9 = 0: k1 = 1: k2 = 1
m1 = 1: m2 = 3: s1 = 3
a1(1) = 0: a1(2) = 1: a1(3) = 2
Sheets("Klad1").Select
t1 = Timer
For j9 = m1 To m2 'a(9)
a(9) = a1(j9)
For j8 = m1 To m2 'a(8)
a(8) = a1(j8)
If a(8) = a(9) Then GoTo 80
a(7) = s1 - a(8) - a(9)
For j6 = m1 To m2 'a(6)
a(6) = a1(j6)
If a(6) = a(9) Then GoTo 60
For j5 = m1 To m2 'a(5)
a(5) = a1(j5)
If a(5) = a(8) Then GoTo 50
If a(5) = a(6) Then GoTo 50
a(4) = s1 - a(5) - a(6)
If a(4) = a(7) Then GoTo 50
a(3) = s1 - a(6) - a(9)
a(2) = s1 - a(5) - a(8)
a(1) = s1 - a(4) - a(7)
' n9 = n9 + 1: GoSub 640 'Print results (selected numbers)
n9 = n9 + 1: GoSub 650 'Print results (squares)
50 Next j5
60 Next j6
80 Next j8
90 Next j9
t2 = Timer
t10 = Str(t2 - t1) + " sec, " + Str(n9) + " Solutions"
y = MsgBox(t10, vbInformation, "Routine LatSqr3")
End
' Print results (selected numbers)
640 Cells(n9, 9).Select
For i1 = 1 To 9
Cells(n9, i1).Value = a(i1)
Next i1
Cells(n9, 10).Value = n9
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
End Sub