# Color an $n\times n$ square with $n$ colors

How many ways is there to color an $n\times n$ square grid with $n$ colors such that each column and each row contains exactly one $1\times 1$ square of each color?

And how many ways if the same is required of the two diagonals?

-
The first part of your Question asks about the number of latin squares of order n, which is this entry in OEIS. I don't know how the counts are reduced if one or both diagonals are also required to be permutations of the n colors (or symbols, to use the more conventional term). –  hardmath Dec 19 '11 at 16:22
According to Handbook of Combinatorial Designs, Volume 10 by C. J. Colbourn, Jeffrey H. Dinitz, Sec. 1.10, the term for latin squares whose diagonals are also permutations (transversals, since these have one each of the rows and columns) is "diagonal latin square". Other authors use the term "double diagonal latin square" to emphasize the restriction for both main diagonal and anti-diagonal. –  hardmath Dec 19 '11 at 16:37

The exact number of latin squares of order n is known only up to n = 11, and these counts are reflected in the OEIS and Wikipedia entries already linked in the Comments.

For counting "diagonal" latin squares I could not find any ready online reference, so I wrote a quick Prolog program that provides solutions for the smaller orders.

Note that for the sake of counting we may assume a particular first row of the latin square, so that if the first row is $(1,2,...,n)$ it is in partially reduced form (reduced form having also the first column in standard order).

For n = 1 there is the one (trivial) case.

For n = 2,3 one quickly finds there are no diagonal latin squares.

For n = 4 there are two cases in partially reduced form. Therefore the total count is 2*4! = 48.

For n = 5 there are eight cases in partially reduced form, for a total count of 8*5! = 960.

My code is not optimized for speed but is clear, so the next step is to refactor and impose constraints in a more efficient manner to make some higher orders feasible.

For n = 6 there are 128 cases in partially reduced form, for a total count of 128*6! = 92160. This result was confirmed by a computation using a different normalization, see discussion below.

For n = 7 my preliminary result is a total count of 171200*7! = 862,848,000 solutions.

Using the partially reduced form (first row in standard order) the n = 6 case was enumerated in 6.3 seconds. Analyzing the time consumed in building stages of the search space suggested that normalizing the main diagonal (in standard order) would be better. I posted a Question here, Counting some special derangements, motivated by how this constrains the choice of anti-diagonals. Another feature of this approach is cutting the enumeration in half via the action of matrix transpose.

These efficiencies reduced the computation time for n = 6 to 1.5 seconds. Despite their use in the n = 7 case, the increased size of the search space led to a running time of 1776 seconds, nearly half an hour.

Such a three-order of magnitude increase in running time suggests that the n = 8 case will not be easily solved on a single CPU, even if the code were compiled instead of interpreted. [Update: For the n = 8 case there are 7361670144*8! = 296,822,540,206,080 diagonal latin squares. This was computed on a single CPU through a combination of speeding up the code that searches on a single main diagonal/anti-diagonal assignment and reducing the number of such assignments by equivalences. The code now runs such a single assignment in 1-2 hours. Given normalization of the main diagonal to the identity sequence $1,2,..,8$, there are 4,752 possibilities (µ-derangements) for the anti-diagonal. A first reduction to 630 equivalences classes is made by using orbits under a dihedral group action. A further reduction to 114 enlarged equivalence classes results from taking "orbits of orbits" under a group isomorphic to $(\mathbb{Z}/2\mathbb{Z})^4$ acting by conjugation. Finally it appears we actually need only count 18 distinct cases due to equivalences of a more contingent nature than those group symmetries. Similar results should apply to solving n = 9, though searching each given µ-derangement will take more time.]

While seeking citations for counts of diagonal latin squares I came across this interesting 2009 paper by Zhang and Ma, Counting solutions for the N-queens and Latin-square problems by Monte Carlo simulations, which is freely available through NIH's Pub Med Central. Perhaps their technique could be adapted for estimating counts of diagonal latin squares of "large" orders.

-
Using some additional symmetries that reduce the search space by more than half, I've redone n=6,7 and confirmed the "preliminary result" for n = 7 given above. The intermediate results hint at further symmetries/equivalences yet to be uncovered, possibly based on the cycle structure of the permutations I called µ-derangements. –  hardmath Jan 24 '12 at 10:17
You should submit these to OEIS –  user1708 Mar 1 '12 at 16:16
@LLLLL, thanks, that's a good suggestion and one I'd not considered. –  hardmath Mar 1 '12 at 18:16
joriki's answer to a related question on centralizers showed that one of the "orbits" I counted for n=8 (see Update above) is actually two distinct orbits. The effect was an undercount, and the given figure 7361670144 should be corrected to 7450228224. –  hardmath Mar 11 '12 at 12:02

In section 7 of this survey paper, Douglas Stones writes:

Comtet said that even estimating $L_n$ when $n → ∞$ “seems to be an extremely difficult combinatorial problem.” However, van Lint and Wilson showed that $$\frac{1}{n} L_n^{1/n^2} → \exp(−2).$$ ... This is not a particularly satisfying result ...

... there is still a long way to go to achieve a ‘clean’ asymptotic solution for the number of Latin squares.

-
+1 for citing Douglas Stones's very readable survey of counting methods for latin rectangles. Note that his Figure 2 tabulates the estimates gotten by Zhang and Ma (cf. my cite) alongside those obtained by others for counts $R_n$ of reduced latin squares, i.e. omitting a factor of $n!(n-1)!$ from the corresponding total counts of latin squares, in the range $12 \le n \le 20$ with $n = 50,100$ only estimated by Zhang and Ma. –  hardmath Jan 3 '12 at 0:47