MrMineev

Attacking Latin Squares with a Devin Subscription

Daniel Alex Mineev

November 2026

This Thursday, while solving Polish 2018 P6, I remembered the well-known fact that a complete graph on an odd number of vertices can be decomposed into Hamiltonian cycles, which led me to wonder how many such decompositions there are. After asking Gemini, I was informed that this is an open problem where the lower and upper bounds are quite far apart from each other. I jokingly told my friend “there ain’t nothing a Devin Max subscription can’t beat” and decided to just have Devin Ultra try to come up with a better bound. After around an hour of thinking it came back with H(K_n) = ((1+o(1)) n/e^2)^{n^2/2} (a claim that has not been formally verified). It then claimed that the method could be used to improve the asymptotics for Latin squares, and several days and a lot of prompting later it had produced improved bounds on the number of Latin squares, together with a Lean formalization.

25,132lines of Lean 4
158Lean files
1,074theorems and lemmas
0sorry remaining
22Devin sessions (1 main, 21 parallel)
28 hoursof Devin working time (17 main, 11 parallel)

Abstract

Let Ln  be the number of Latin squares of order n  . The classical bounds, from the van der Waerden permanent bound and Brégman’s theorem, determine Ln  up to a factor eΘ(nln2n)  . We prove

 2          3                   2
n (ln n− 2)+ 2 nlnn − n ≤ ln Ln ≤ n (ln n− 2)+ cnlnn+ O(n)

for an explicit constant c  , so         2 n2Θ(nlnn)
Ln = (n∕e ) e  , and then that the lower bound gives the right coefficient,

ln Ln = n2(ln n− 2)+ (32 +o(1))nln n.

Both statements are formally verified in Lean 4. Finally we describe a claimed explicit rate, which is not formalized:

ln Ln = n2(lnn − 2) + 32 nlnn+ O (n (lnn)11∕12(lnln n)1∕12).

Section 1 gives the proof of the O (n ln n)  gap in full. Sections 2 and 3 are overviews: they state the key theorems behind the two sharper results and show how they combine; the proofs of these theorems are in the accompanying papers [1, 2].

The result extends to Latin rectangles. Let L(k,n)  be the number of k× n  Latin rectangles. For every k = k(n)< n  ,

              k (n)kn    k-−n∕2 −k∕2
lnL (k,n)= ln[(n!)( nk )(1− n )   e   ]+ o(n lnn),

so the formula of Godsil and McKay, known to be asymptotically exact for k = o(n)  , gives L(k,n)  up to a factor  o(nlnn)
e  for every number of rows. Away from squares the error is smaller: for all k ≤ n− 3  the two sides differ by at most 137n ln -n-
       n−k  , so for k ≤ (1− η)n  the formula gives L (k,n)  up to a factor eOη(n)  . Both statements are also formally verified in Lean 4; they are described in Section 5 and proved in [3].

Authorship. This overview was written with AI assistance (Devin, an AI system built by Cognition AI). The author has worked through only the proof of the O(nlnn)  gap in Section 1; Sections 2 and 3 summarize results produced by the AI system, which the author has not verified. The same holds for Section 5. The (32 + o(1))  theorem and the two rectangle theorems are certified by the Lean proof.

Contents

 1 O(nlnn)  gap
  1.1 Constants
  1.2 Overview
  1.3 Row Bound
  1.4 Exchangeability
 2 o(n ln n)  gap overview
  2.1 Theorem A: dense rows
  2.2 Theorem B: intercalates
  2.3 Theorem C: transfer to a tree
  2.4 Theorem D: the tree limit
  2.5 How the theorems combine
 3 Explicit rate overview
  3.1 How the theorems combine
  3.2 What limits the rate
 4 General Probabilistic Tools
5 Latin rectangles

This paper will take the entropy-based perspective on Latin squares, redefining it as a collection of bijections {σi}i∈[1,n]  from C  to S  (finite sets, both of size n  ) such that σi(x) ⁄= σj(x)  for any i,j ∈ [1,n]  , where each σi  represents the i  th row as a bijection between the numbers {1,...,n } and the columns! Choosing an arbitrary Latin square X  uniformly at random gives that,

              ∑n
ln Ln = H(X ) =   H (σi|σ1,...,σi−1)
              i=1

where H(X )  denotes the Shannon entropy of the random variable. Observe that H(σn|σ1,...,σn−1) = 0  since the last color is forced. The H (σn−1|σ1,...,σn− 2)  may be bounded by using that Gn −1  is a 2  -regular bipartite graph, thus it consists of at most n∕2  even cycles and consequently ln 2n∕2 = n ln 2
        2  .

1 O(nlnn)  gap

This section will focus on the following proving the following result,

Theorem 1. Let Ln  denote the number of Latin squares of order n  . For every integer n ≥ 1  ,

 2          3                     2
n (ln n− 2)+ 2 nlnn − n ≤  lnLn  ≤ n (lnn − 2) +153 nlnn +193 n.

In particular,

        2
ln Ln = n (lnn − 2)+ Θ(nlnn).

The theorem is unconditional and has been formally verified in Lean 4. The constant 153  is unoptimized, in fact it can be improved quite substantially with a number of different techniques, however since the next section is focused on showing that it may be optimized to 32 + o(1)  I see no reason to overcomplicate the proof.

1.1 Constants

Throughout the proof many constants are used, this is a quick table reference to them. Let B
 k  denote a Bin(k,s)  random variable.

e1 = 𝔼 [1+1Bd−1] e2 = 𝔼 [2+B1d−2]
ℰ(s) = e21 +e2(1− e1)  b(s) = ℰ1(s)
β(s) = 𝔼 [ln(1 + Bd−1)]          [               ]
κ(s) = 𝔼 (2+B---)12(3+B---)2
             d−3     d−3
         [-------1-------]
κ0(s) = 𝔼 (1+Bd−2)2(2+Bd−2)2     ∫ 1
τd = 0 (2β + lnℰ)ds
     ∫
Ad =  10 b(1 − s)s2κ ds       ∫
A0d = 10 b(1 − s)sκ0ds

1.2 Overview

Let us define Gi  (a bipartite graph with V(Gi) = C ∪ S  ) as the available option graph after i  steps, or more formally Gi = σi+1 ∪ ...∪ σn  , where one interprets each σj  as a perfect matching from C  to S  .

The idea of counting the number of matchings through the following two quantities,

Figure 1

Proposition 1 (Row Bound). Let (P,G, σ)  be jointly distributed, with G  a function of P  , where G  is a d  -regular bipartite graph on C ∪S  , d ≥ 3  , and σ  is a perfect matching of G  . Then

                                0   0
H (σ | P ) ≤ nτd + Ad 𝔼Q (G,σ)+ A d𝔼Q (G, σ).

Proposition 2 (Exchangeability). Let X = (σ1,...,σn)  be a uniformly random Latin square. For the row i  , with d = n− i+ 1  ,

𝔼 Q(Gi−1,σi) ≤ d3 + d2.

Moreover Q0 (G,σ) ≤ n (d − 1)  for every d  -regular G  and perfect matching σ  of G  .

Thus, letting Hd =  H(σi | σ1,...,σi−1)  (where d = n− i+ 1  ) the main inequality used in this section is,

|---------------------------------------------------|
|Hd ≤ n (lnd − 1)+(K ∗+ K ∗)n+  lnd +O (n-ln-d)+ O(1) |
|     ◟---◝◜--◞  ◟--0-◝◜-1-d◞  f◟ro◝m◜◞Q      d2          |
|      main term  from τd and Q0                      |
----------------------------------------------------

Lemma 1. The following inequalities on the coefficients hold,

  1. For d ≥ 3  :                ∗
τd ≤ ln d− 1+ K-0 + O( 1-)
              d      d2  , where K ∗= ln2π+ J
 0  .
  2. For d ≥ 3  :      3   2
Ad (d + d ) ≤ lnd+ O (1)  and  0   K-∗1     ln-d
Ad ≤ d2 + O (d3 )  .

This constant inequality (Lemma 1) is nothing particularly interesting and for the exact proof refer to the AI generated writeup in the accompanying files.

These inequalities together produce a tighter gap,

       ∑n                                                         ∑
lnLn =    H (σi|σ1,...,σi− 1) ≤ H(σn|σ1,...,σn−1)+ H (σn− 1|σ1,...,σn−2)+    (nτd+ Ad𝔼Q (G, σ)+ A0d𝔼Q0 (G,σ))
       i=1                                                         d≥3
                    n-     ∑              nK0∗    (-n)       3   2    0
                  ≤ 2 ln 2+    (n(lnd − 1) + d  + O  d2  + Ad(d + d )+ Ad n(d − 1))
                           d≥3                ∗           ∗    (     )
                   ≤ n-ln2+ ∑  (n(ln d− 1)+ nK-0 +lnd + nK-1+ O   nlnd- + O (1))
                     2      d≥3              d          d         d2
                     n                   1
                   = --ln2+ n2(lnn − 2) +-nlnn + K∗0n ln n+ lnn!+ K ∗1nlnn + O(n)
                      2                  2                |------------(-----------)-------------|
                                                        = n2(lnn − 2) +  3 + K∗ + K∗  nlnn + O(n) |
                                                          --------------2----0----1--------------|
where it was used that ln n! = n lnn − n+ 12 ln n+ O (1)  (Stirling), so that ∑
  d≥3 n(lnd − 1) = nlnn!− n2 + O(n) = n2(lnn − 2) + 12nlnn + O(n)  and ∑    lnd = ln n!+ O(1) = n ln n+ O (n)
  d≥3  ; that ∑n   1 = ln n+ O (1)
  d=1d  ; and that ∑    ln2d< ∞
  d≥1 d . The first two terms are bounded by 0+ n ln 2
   2  , as shown at the start.

1.3 Row Bound

Let t  be some ordering then                     ∑
H (σ|P) = H (σ|P, t) =  u∈C H (σ(u )|P,t,(σ(c))tc<tu)  and let U  = {c : tc ≥ tu} . The idea is to select a value based on its popularity among other still searching columns, this is done by taking the probability distribution q(w) = (1∕Mw )∕Z  where Mw = |N(w )∩ U| and     ∑
Z =   w∈A 1∕Mw  (where A  is the set of available values) is a normalization constant. By Gibb’s inequality (let p(w) = ℙ(σ(u) = w |P,t)  ),

 ∑                         ∑                    [∑   (            )]
    H (σ(u)|P,t,(σ(c))tc<tu) ≤    𝔼p[− lnq(σ(u))] = 𝔼p    lnM σ(u) + lnZ
u∈C                       u∈C                    u∈C
(1)

Lemma 2. Let us fix G  , σ  and u  , then 𝔼Z = 1  (where t  is the only random variable).

Figure 2

Proof. Let σ(u) = w0  . Observe that one may write Z  in terms of indicator functions as     -1--  ∑     ξσ−1(w)
Z = Mw0 +   w∈W   Mw  where W = N (u)∖{σ (u)} and ξa = 1[a ∈ U]  . A similar rewrite may be done with Mw  ,

                   ∑              ∑
Mw  = |N (w)∩ U | =      ξx = 1+          ξx
                  x∈N(w)       x∈N(w)∖{u}

Thus, Mw  is the sum a constant and d− 1  i.i.d. Bernoulli random variables and thus,

 [ξπ(w)]    1    [Mw − 1]     1  (     [  1 ])     1  (     [    1    ])    1− e1
𝔼 -M--- =  d−-1𝔼  --M---- = d-−-1 1 − 𝔼  M---  = d−-1- 1 − 𝔼 1-+-B----   =  d−-1--
     w               w                    w                       d−1,s

where      [       ]
e1 = 𝔼 ---1---
       1+Bd−1,s (s  is the probability that ξw  is 1  ). Thus,

𝔼Z = e  + |W |⋅ 1-−-e1-= e + (1− e ) = 1
      1        d− 1    1       1

where 1∕Mw0   per definition has an expectation of e1  . □

Let us fix G  , σ  and u  , then since ln z ≤ lny + z∕y − 1  (because x ≥ lnx+ 1  ) a bound on 𝔼lnZ  may be obtained with y = N∕b(s)  (where N = |A | ),

          [      ]    [      ]                          [   ]
𝔼 [lnZ ] ≤ 𝔼 ln-N- + 𝔼  Z-⋅b(s)- − 1 = 𝔼[ln N]− lnb(s)+ b(s)𝔼 Z- − 1
             b(s)        N                                N
(2)

Observe that          [                   ]
𝔼[Z-] = 𝔼 --1--+ ∑w ∈W  ξσ−1(w)-
  N       NMw0          NMw . Let us bound the terms independently,

  1. Let X  = 1-
     N  and Y = --1-
    Mw0   then by the Covariance Bound (Proposition 5),

      [  1   ]               [ 1 ] [  1 ]
𝔼  N-M--- = Cov(X, Y)+ 𝔼  N- 𝔼  M---  ≤ c0s(1− s)κ0(s)+ e21
      w0                          w0
  2. The other terms are bounded similarly using the Covariance Bound (Proposition 5),

      [ξσ−1(w )]     [   1             ]
𝔼  -N-M--  = s𝔼  NM---| ξσ−1(w ) = 1 ≤ se22 + cws2(1− s)κ
       w            w

Thus, (using e21 + |W |se22 = e21 + (d− 1)se22 = e21 + e2(1 − e1) = ℰ )

  [  ]
𝔼  Z- ≤ (c s(1 − s)κ  + e2)+  ∑  (se2+ c s2(1 − s)κ) = ℰ + (1− s)(cκ + ∑   c κ) = ℰ + s(1 − s)(c κ + Q sκ)
   N      0       0   1   w∈W    2   w                      0 0   w∈W  w                0 0   A

The value of 𝔼[ln N ]  in Equation 2 is simply β(s)  (since N  is the sum of Bernoulli variables) and thus,

                                     2
𝔼 [lnZ] ≤ β(s) +lnℰ + b(1 − s)(c0sκ0 + Qus κ)

Thus, looking back at Equation 1 one is left with the task of integration over s  and taking the expectation.

Recall that everything above was computed for fixed G  , σ  , u  and conditionally on tu = 1− s  , so that the indicators      ξx  , x ⁄= u  , are i.i.d. Bernoulli(s)  . Since Mw  = 1+ ∑           ξx
  0        x∈N(w0)∖{u}  has the same distribution as N  , we have 𝔼[ln M σ(u)] = β(s)  as well, and therefore

𝔼 [lnM σ(u) +lnZ | tu = 1− s] ≤ 2β(s)+ ln ℰ(s) +b(s)(1 − s)(c0(u)sκ0(s) + Qus2κ(s)).
(3)

The time t
u  is uniform on [0,1]  , hence so is s  , and integrating (3) over s  gives

                  ∫                  ∫                     ∫
                    1                  1        2            1                            0
𝔼t[lnM σ(u) + ln Z] ≤ 0 (2β + lnℰ)ds+ Qu 0 b(1 − s)s κ ds+ c0(u) 0 b (1 − s)s κ0ds = τd +Ad Qu + Ad c0(u),

Now sum over u ∈ C  . By definition cw = |{w ′ ∈ W ∖ {w } : σ −1(w ′) ∼ w}| and c0(u) = |{w ′ ∈ W : σ− 1(w ′) ∼ σ (u)}| , so

∑        ∑   ∑                  ∑
    Qu =        cw = Q(G,σ ),       c0(u) = Q0 (G, σ),
u∈C      u∈Cw∈W                 u∈C

where Q(G,σ)  is the number of triples     ′
(u,w ,w)  with   ′
w ,w ∈ N (u) ∖{σ(u)} ,      ′
w ⁄= w ,   −1  ′
σ   (w ) ∼ w  , and   0
Q  (G, σ)  is the number of pairs     ′
(u,w )  with   ′
w  ∈ N(u)∖ {σ(u)} ,  −1  ′
σ  (w ) ∼ σ(u)  . Hence, for fixed G  and σ  ,

   [∑                ]
𝔼t     (lnM σ(u) + ln Z) ≤ n τd + Ad Q(G,σ) +A0dQ0(G, σ).
    u∈C

Finally take the expectation over (G,σ )  in (1) to obtain Proposition 1!

1.4 Exchangeability

Let us split Proposition 2 into two parts,

Lemma 3. Q0 (G, σ) ≤ n(d − 1)

Proof. Since for u ∈ C  there are no more than d− 1  values w ′ ∈ S  with w ′ ⁄= σ(u)  , thus giving the trivial bound of n(d − 1)  ! □

Lemma 4. 𝔼Q (Gi−1,σi) ≤ d3 + d2   (over the random Latin square X  ), where d = n − i +1

Proof. Let a row be late if it comes after i  . Write G = Gi−1  and σ = σi  . Then, there are two options either all the edges (y,w)  , (u,w )  and     ′
(u,w )  are in different matchings, or     ′
(u,w )  and (y,w)  are in the same matching.

Figure 3

Since X  doesn’t depend on the order, X  can be generated by first choosing the unordered set 𝒟 of its n  rows and then putting them in a uniformly random order. For a row M ∈ 𝒟 and j  distinct rows R1,...,Rj ∈ 𝒟 ∖ {M } ,

                                1  d− 1  d − 2   d− j   1  d j
Pr[σi = M and R1,...,Rj are late] = n-⋅n−-1-⋅n-−-2 ⋅⋅⋅n−-j-≤ n-(n) :
(4)

The inequality holds because dn−−ll ≤ dn  for d ≤ n  . Observe,

            ∑
𝔼[Q | 𝒟 ] =      Pr[σi = M and the rows of e1,e2,e3 are late]
          (M,y,u,w)

So it remains to bound the probability of each configuration by (4), and to count the configurations.

Type (a): e1,e2,e3   lie in three distinct rows. These three rows must all be late, so by (4) with j = 3  each such configuration has probability at most 1 d 3
n(n)  . There are at most   4
n  configurations in total (n  choices for each of M, y,u,w  ), so type (a) contributes at most

n4 ⋅ 1(d-)3 = d3.
    n n

Type (b): e1   and e2   lie in the same row R  . Now only two distinct rows must be late (R  and the row of e3  ), so by (4) with j = 2  each such configuration has probability at most 1(d)2
n n  , which is larger than in type (a). There are at most n3  (since one needs to only fix M  , y  and u  ) configurations of type (b), and they contribute at most

 3  1-d-2    2
n  ⋅n(n ) = d .

Adding the two contributions,           3   2
𝔼[Q | 𝒟] ≤ d +d  for every 𝒟 , and averaging over 𝒟 gives the claim. □

Thus Lemma 3 and Lemma 4 give Proposition 2!

2 o(n ln n)  gap overview

This section is an overview of the proof of

Theorem 2.

ln Ln = n2(lnn − 2)+ (3+ o(1))nlnn.
                    2

The theorem has been formally verified in Lean 4 with no hypothesis (theorem mainStatement, for the count Erdos725.L n n of the library formal-conjectures). The full written proof is in [1]; here only the key theorems are stated, with a few words on why each holds, followed by the calculation that combines them.

The lower bound lnLn ≥ n2(ln n− 2)+ 3n ln n− n
                   2  follows from the permanent inequality of Gurvits [11] applied row by row. So everything is about the upper bound. The bound used in the previous section was,

Hd ≤ n(lnd− 1) +κ n-+ ln d+ ...,
                  d

and its three terms sum to 1
2n lnn  , κn ln n  and nlnn  . To reach 3
2  two things must be improved: the term ln d  must become       1
lnd − 2 lnn  on most rows, and the constant κ  must be brought down to 1
2  . The first is done by Theorem A below, the second by Theorems B, C and D:

A◟:-den◝se◜-rows◞    B◟: intercalates-=⇒-◝C◜:-transfer-to-a-tree◞   D◟:-tre◝e◜ limit◞
ln d→lnd− 12lnn           κ→ κψ for any local rule ψ        infψκψ≤ 12

2.1 Theorem A: dense rows

For rows with d ≥ ηn  a different predictor is used (guess w  with probability proportional to N ∕|U| instead of 1∕Mw  ).

Proposition 3 (Theorem A, dense row bound). For a uniformly random Latin square and 4 ≤ d ≤ n  ,

Hd ≤ n(ln d− 1)+ lnd− 1 ln n+ 29 n.
                     2         d

The constant 29  of nd  is far worse than in Proposition 1, which is why this bound is only used for d > ηn  , where ∑
  nd = Oη(n)  .

2.2 Theorem B: intercalates

An intercalate of a Latin square is a 2× 2  Latin subsquare, i.e. two rows and two columns whose four cells contain only two symbols. Let I(X )  be their number. In Section 1 the quantity Q0  was bounded trivially by n(d− 1)  . In fact 𝔼 Q0 ≤ d2 + 4d2 𝔼 I(X)
           n  by the argument of Lemma 4, where the second term comes from the configurations in which the two late edges lie in the same row. The same configurations appear in every pattern count of Theorem C, so a bound on 𝔼 I(X)  is needed.

Theorem 3 (Theorem B, intercalate bound). For every n ≥ 1  and a uniformly random Latin square X  of order n  ,

𝔼I(X ) ≤ 496n2(1+ lnn)2.

The proof is independent of everything else and has three steps.

The factor ℓn = (1+ lnn)2  is harmless: it only enters the row bounds through ℓn∕d ≤ n∕d2 + ℓ2n∕n  , and ∑d ≤n ℓn∕d = O(n)  .

Remark. Kwan, Sah and Sawhney [10] proved a large deviation bound which gives 𝔼I(X ) ≤ n2∕2  for all large n  . With their theorem, Theorem B is not needed and the factor ℓn  disappears from all calculations. Their theorem is not used here, so that the proof (and its Lean verification) rests on no result outside the standard library.

2.3 Theorem C: transfer to a tree

For rows with d ≤ ηn  the columns are revealed in a random order as in Proposition 1; a column revealed at time 1 − s  sees about λ = ds  available symbols. The interval of λ  is cut in three, for constants 0 < 𝜃 < 1 < Λ  :

Let 𝒯 λ  be the following random rooted tree with a perfect matching. The root (a column) is matched to one marked child and has Po(λ)  further children; a vertex matched to its parent has Po(λ)  children; any other vertex is matched to one of its children and has Po (λ)  further children. The cost of a local rule ψ  on the tree is C ψ∞(λ) = 𝔼 [− ln ψ(marked child)]  , and we put

                    ∫ Λ                    ∫ Λ
κ (𝜃,Λ) = K ∗− I(Λ)+    Cψ (λ)dλ,    I(Λ) =    (2𝔼 ln(1 +P  )+ ln ℰ (λ))dλ,
 ψ         0         𝜃   ∞                  0           λ      ∞

where Pλ  is a Poisson variable of mean λ  and ℰ∞ (λ)  is the limit of ℰ (s)  as d → ∞ with ds = λ  . The term − I(Λ )  is what is saved by not using the rule 1∕Mw  for λ < Λ  , and the integral is what the local rule costs instead.

Theorem 4 (Theorem C, transfer). Let ψ  be a local rule and 0 < 𝜃 < 1 < Λ  , 𝜖 > 0  . There is d0   such that for d0 ≤ d ≤ n  ,

                            n             ∘ ----   n
Hd ≤ n(ln d− 1)+ (κψ(𝜃,Λ)+ 𝜖)d + lnd + O(1+  n∕d + d3∕2).

The proof couples the ball around u  in the leftover graph of a random Latin square with 𝒯ds  . The two differ only when the exploration of the ball meets a vertex twice, and the expected number of such repeats is bounded by counting patterns in a random Latin square, as in Proposition 2. The patterns that contain two edges of the same row are controlled by Theorem B. The 𝜖  comes from mixing ψ  with the uniform guess, so that the cost of a wrong prediction is bounded.

2.4 Theorem D: the tree limit

The best rule of radius r  (the cavity rule) guesses the conditional law of the marked child given the tree up to depth r  . Its cost cr(λ)  decreases in r  , to a limit c∗(λ)  , so

                       ∫
infκ (𝜃,Λ) = K ∗− I(Λ)+  Λ c(λ)dλ.
 ψ  ψ         0         𝜃   ∗

Theorem 5 (Theorem D, tree limit). For every δ > 0  there are 𝜃  , Λ  and a local rule ψ  with           1
κ ψ(𝜃,Λ ) ≤ 2 + δ  .

By the lower bound and Theorem C, κ  ≥ 1
 ψ   2  for every local rule, so Theorem D is sharp. It rests on three facts about  𝒯
  λ  . Let ℓ,ℓ′ be independent copies of the likelihood ratio of the subtree below a child of the root, A(λ) = 𝔼ln(1 + ℓℓ′∕λ)  and

G(λ) = c∗(λ)− λA(λ).

  1. (Normalisation) K0∗− I(Λ) = − (Λ lnΛ − Λ)+ O(1∕Λ)  .
  2. (Bethe inequality) For 0 < 𝜃 ≤ Λ  and every  ′
𝜖 > 0  , ∫
  Λ                      ′
 𝜃  c∗(λ)dλ ≤ ΛG (Λ)+ 𝜃+ 𝜖 , up to replacing Λ  by Λ + h  on the right with h  small. Its proof uses that the laws of ℓ  are a fixed point of the tree recursion, that G  is non-decreasing, and a second-order expansion of the functional G  around the fixed point, written with Laplace transforms. (Whether equality holds for 𝜃 = 0  is not known; numerically it does.)
  3. (Large Λ  ) G(λ) ≤ lnλ − 1+-1-+ 14
               2λ   λ2  for λ ≥ 1  . This holds because G  is at most its value for the tree of depth one, which is an explicit Poisson sum.

Together,

inψfκ ψ ≤ − (Λ lnΛ − Λ )+Λ G(Λ)+ 𝜃+ 𝜖′+O (1∕Λ) ≤ − (Λ lnΛ− Λ)+ Λ ln Λ− Λ + 12+𝜃 +𝜖′+ O(1∕Λ) = 12+ 𝜃+ 𝜖′+ O(1

and one takes 𝜃  , ′
𝜖 small and Λ  large. Note where the 1
2  comes from: it is the term 1-
2λ  in fact 3, multiplied by Λ  .

2.5 How the theorems combine

Fix δ > 0  and take ψ,𝜃,Λ  from Theorem 5, then η > 0  small. Use Proposition 3 for d > ηn  , Theorem 4 (with 𝜖 = δ  ) for d0 ≤ d ≤ ηn  , and the trivial bound Hd ≤ n ln d  for the O(1)  rows with d < d0  :

       ∑n      ∑n             ∑          n         ∑                 n
lnLn =    Hd ≤    n(ln d− 1)+     ((12 + 2δ)d + lnd)+   (ln d− 12 ln n+ 29-d)+ O(n)
       d=1     d=1           d≤ηn                 d>ηn
                   ≤ n2(lnn − 2) + 12nlnn +(12 + 2δ)n ln n+ η nlnn+ 12nlnn +O δ,η(n)
                                                            |------------------------------------|
                                                          = n2(lnn − 2)+ (3+ 2δ+ η)nlnn + Oδ,η(n) |
                                                            --------------2----------------------|
where it was used that ∑                        2    2          1
  d≤nn(lnd− 1) = nln n!− n = n (lnn − 2) + 2nlnn + O(n)  (Stirling), ∑     1
  d≤ηnd ≤ ln n+ 1  , ∑
  d≤ ηn ln d ≤ η nlnn  , ∑
  d>ηn(lnd − 12 lnn) ≤ 12nlnn  , ∑
  d>ηn nd ≤ nη  , and ∑  ∘ ----
  d( n ∕d + nd−3∕2) = O(n)  . Since δ  and      η  are arbitrary, the coefficient of n ln n  is 3+ o(1)
2  .

So the three halves are: 1
2  from Stirling’s formula in the main term, 1
2  from the tree constant (Theorems B, C, D), and      1
     2  from lnd−  1ln n
      2  on the dense rows (Theorem A).

3 Explicit rate overview

The proof of the previous section gives no rate for the o(1)  : in Theorem D the radius of the rule ψ  is not controlled as   δ → 0  , because cr → c∗ is only known as a limit, and the constants of Theorem C depend on ψ  in an unspecified way. The note [2] makes both quantitative.

Theorem 6 (claimed).

ln Ln = n2(lnn − 2)+ 3 nlnn +O (n(ln n)11∕12(lnlnn)1∕12).
                   2

Status. Unlike Theorems 1 and 2, this theorem is not formalized in Lean, and its proof has not been checked independently. It should be read as a claim.

The lower bound already has the error O(n)  , so again only the upper bound matters. Theorems A and B are used unchanged; Theorems D and C are replaced by the following two.

Theorem 7 (Theorem D′ , Bethe inequality at finite depth). There is a constant C
 1   such that for every integer r ≥ 2  , with Λ = r1∕12   and 𝜃 = r− 1∕12   , the cavity rule of radius r  satisfies

           ∫ Λ
K ∗0 − I(Λ) +   cr(λ)dλ ≤ 1 + C1r−1∕12.
            𝜃           2

This is Theorem D with a rate. It is not obtained from a rate for cr → c∗ , which is not known. Instead the proof of the Bethe inequality is repeated at finite depth, where the laws of the likelihood ratio at depth j  are no longer a fixed point of the recursion. The amount δj  by which they fail to be one is a difference of a bounded monotone sequence, so ∑  δj ≤ 1
  j  , and among the depths j ≤ R  there is one with defect at most of order 1∕R  . Since cr  is non-increasing in r  , that depth suffices. Balancing the resulting error O (1∕(hR ))  against the other small parameters of the Bethe inequality gives the exponent -1
12 .

Proposition 4 (Theorem C′ , transfer with explicit constants). There are constants C0,d∗ such that for every row with d∗ ≤ d ≤ n  , every integer r ≥ 2  , all 0 < 𝜃 ≤ 1
       2   , Λ ≥ 1  with 2Λ(1+ Λ) ≤ d  , 0 < 𝜖 ≤ 1
       2   and every local rule ψ  of radius r  ,

                     n-                      ℓn  n-Λ3
Hd ≤ n(ln d− 1)+ ln d+ d [κψ(𝜃,Λ) +2Λ 𝜖]+ C0 (1 + d +  d2 )
                                          ∘ n-   2n   45√n-ℓn                                  e
                                 + Tr(Λ,𝜖)(  d-+ d3∕2 +---d---),   Tr(Λ,𝜖) = (2r+ 4)2r+5 (3Λ )r+3 ln 𝜖.

This is Theorem C with the dependence on the rule made explicit: the number of patterns that can occur in a ball of radius     r  is eO(rlnr)  , and this is the factor T
 r  . It multiplies ∘n-∕d-+ nd−3∕2  , which is small compared with n
d  when both d  and   n∕d  are large. So the radius may grow with n  , and may depend on the row.

3.1 How the theorems combine

Let η = 1∕lnn  .

Summing over the sparse rows, ∑ lnd ≤ ηn ln n = n  , ∑  n-≤ 1n ln n+ n
   2d   2  , all terms inside the O (⋅)  sum to O (n)  , and the remaining term is

  ∑    n     −1∕12      ∫ ln n lny 1∕12             11∕12      1∕12
       -d r(d)   = O (n     (-y-)   dy) = O(n (lnn)   (ln ln n)   ),
d1≤d<ηn                 1

after substituting y = ln d  for     √--
d ≤  n  and       n
y = ln d  for    √ --
d >  n  . With ∑              2           1
  dn(ln d− 1) = n (lnn− 2)+ 2nlnn +O (n)  this gives

|--------------------------------------------------|
|       2          3                11∕12      1∕12 |
lnLn ≤ n (ln n− 2)+ 2 nlnn + O(n(lnn)    (lnlnn)   ) |
----------------------------------------------------

3.2 What limits the rate

The only term that is not O (nlnln n)  is the one with    −1∕12
r(d)  , so the rate is decided by the exponent in Theorem D ′ .

4 General Probabilistic Tools

Proposition 5 (Covariance Bound). Let (ξi)  be i.i.d. Bernoulli(s)  , let I1,I2   be index sets with |I1| = |I2| = k  and |I1 ∩ I2| = c  , let      ∑
Sj =   i∈Ij ξi  , and let f1,f2 : {0,...,k} → ℝ  . With Δf(j) = f (j +1) − f (j)  ,

                                         ∘------------------------
|𝔼f1(S1)f2(S2)− 𝔼f1(Bk)𝔼f2 (Bk )| ≤  cs(1− s) 𝔼 Δf1(Bk− 1)2 𝔼Δf2 (Bk −1)2.

Proof. Let I1 ∩ I2 = {1,...,c} , let ξ′i  be independent copies of the ξi  , and let   (j)
S2  be S2  with ξ1,...,ξj  replaced by ξ′1,...,ξ′j  . Then S(20) = S2  , and S(2c)  is independent of S1  and distributed as Bk  . So the left side equals |∑c   𝔼 [f1(S1)(f2(S(j−1))− f2(S(j)))]|
   j=1            2          2 . Fix j  and write S1 = S′ + ξj
     1  ,   (j−1)
S2    = T + ξj  ,  (j)       ′
S2  = T + ξj  , where   ′
(S1,T)  is independent of     ′
(ξj,ξj)  and   ′
S1  , T  are each distributed as Bk −1  . Then    (j−1)      (j)
f2(S2   )− f2(S2 ) = (ξj − ξ′j)Δf2(T)  and f1(S1) = f1(S′1)+ ξj Δf1(S′1)  . Since 𝔼 [ξj − ξ′j] = 0  and 𝔼 [(ξj − ξ′j)ξj] = s(1 − s)  ,

            (j−1)       (j)
𝔼[f1(S1)(f2(S 2   )− f2(S2  ))] = s(1− s)𝔼[Δf1(S′1)Δf2(T)].

Apply the Cauchy–Schwarz inequality to each of the c  terms. □

5 Latin rectangles

A k × n  Latin rectangle is a k × n  array with entries in {1,...,n } and no repeated symbol in a row or a column; L (k,n)  is their number, so Ln = L (n,n)  . Erdős Problem 725 [14] asks for a formula A(k,n)  with L(k,n)∕A(k,n) → 1  for all k ≤ n  .

Known results. With (n)k = n(n − 1)⋅⋅⋅(n− k + 1)  , Godsil and McKay [15] proved for       6∕7
k = o(n  )  that

                        (n )      k
L(k,n) ∼ GM (k,n) = (n!)k(--kk )n(1−--)−n∕2e−k∕2,
                        n        n

and Li [16] extended this to all k = o(n)  . For k  proportional to n  we found only the classical bounds from the van der Waerden and Brègman theorems,

                           n
(n!)k((n-)k)n ≤  L(k,n) ≤    ∏    (d!)n∕d,
     nk                 d=n −k+1

whose logarithms differ by 12(ln11−α − α)nlnn +O (n)  for k = ⌊αn⌋ , and by Θ(n ln2n)  for k = n  .

The new results. They are proved in [3] and formalized in Lean.

Theorem 8. For all 0 ≤ k < n  ,

|ln L(k,n)− ln GM (k,n)| ≤ 3032-nlnn + 198 n,

and lnL(k,n) = lnGM (k,n)+ o(n lnn )  for every sequence k = k(n) < n  .

So the formula of Godsil and McKay gives L(k,n)  up to a factor  o(nlnn)
e  for every number of rows. For k = ⌊αn⌋ this determines the coefficient of n ln n  in lnL(k,n)  : the classical lower bound is correct up to  o(n lnn)
e  . For k = o(n)  the theorem is much weaker than [15, 16], and in no range does it give the factor 1 + o(1)  that Problem 725 asks for.

Theorem 9. For all 0 ≤ k ≤ n − 3  ,

|lnL(k,n)− lnGM (k,n)| ≤ 137n ln-n---.
                               n− k

In particular the difference is at most 137ln(1∕η) ⋅n  for k ≤ (1− η)n  , and at most 274 k  for k ≤ n ∕2  .

So for k  proportional to n  the formula of Godsil and McKay gives L(k,n)  up to a factor eO (n)  , where the classical bounds differ by eΘ(n lnn)  . This is still far from the factor 1+ o(1)  of Problem 725, and an error of order n  in ln L(k,n)  is the limit of the method, because the lower and upper bounds for one row differ by a constant factor. Theorem 9 is the stronger of the two when n − k = n1−o(1)  and Theorem 8 when n− k ≤ n1−c  .

Proof of Theorem 8. Let P  = dn d!(d−1)(d−1)(n−d)
  d     dd  d  . By the permanent inequality of Gurvits [11], every Latin rectangle with d  unused symbols in each column can be extended by one row in at least P
 d  ways, so L (k + 1,n ) ≥ L(k,n)P
                   n−k  . Using this for the first k  rows and for the last n − k  rows,

  ∏n                      Ln                            ∑n                 ∑n
       Pd  ≤ L(k,n) ≤  ∏n-−k---,   so    0 ≤ lnL(k,n)−       lnPd ≤ lnLn −    ln Pd.
d=n−k+1                  d=1 Pd                       d=n−k+1              d=1

The right side does not depend on k  , and it is o(nlnn)  : this is the theorem for squares together with ∑            2          3
  d≤n ln Pd ≥ n (lnn − 2)+ 2nlnn − n  , which is how the lower bound for squares is proved. Finally ∑n
  d=n− k+1ln Pd = lnGM (k,n) +O (n)  uniformly in k < n  , by Stirling’s formula. Any sharper bound for squares, such as the one of [2], carries over to all rectangles in the same way.

Proof of Theorem 9. Here the upper bound is also obtained row by row. Let d = n− k ≥ 4  . The lower bound for one row is L (k + 1,n ) ≥ L(k,n)Pd  as above. For the upper bound, the entropy bound for rows with many free symbols (the dense row bound of [1]) is applied to the last row σ  of a uniformly random (k+ 1)× n  Latin rectangle, given the first k  rows:

                                                  n
lnL(k+ 1,n) ≤ ln L(k,n )+ n(lnd − 1)+ ln d− 12 lnn + 137d-.

The proof of that bound uses the Latin square only through one estimate: the expected number Q  of triples (u,x,w )  , with u,x  columns and w  a symbol, such that w  is unused in u  and in x  and σ(x)  is unused in u  , must be at most about d3  . For squares this came from the symmetry between all n  rows. For rectangles it follows from the Cauchy–Schwarz inequality. Let X  be an m × n  Latin rectangle, D = n− m  , and c(u,x)  the number of symbols unused in both columns u  and x  . Summing Q  over the choice of the row σ  among the m  rows of X  gives

∑  c(u,x)(D − c(u,x )) ≤  nD3 − D4 = mD3,
u,x

because ∑
  u,xc(u,x) = nD2  and so ∑
  u,xc(u,x)2 ≥ D4  . The rows of a uniformly random rectangle can be permuted, so the expectation of Q  for the last row is at most D3  . Finally both one-row bounds differ from lnGM (k + 1,n )− ln GM (k,n )  by at most 137n∕d  , and ∑j<k -n- ≤ nln-n-
     n−j      n−k  .

References

[1]   The number of Latin squares: lnLn = n2(ln n− 2)+ (32 +o(1))nlnn  , accompanying paper (latin_squares.pdf), with the Lean formalization in the repository.

[2]   An explicit error term for the number of Latin squares, accompanying note (latin_rate.pdf).

[3]   The number of Latin rectangles: the Godsil–McKay formula for every number of rows, accompanying note (latin_rectangles.pdf).

[4]   J. H. van Lint, R. M. Wilson, A Course in Combinatorics, Cambridge University Press, 1992, Chapter 17.

[5]   L. Kopfová, M. Kwan, R. Montgomery, K. Petrova, Asymptotic probabilities in random Latin squares, arXiv:2611.24564 (2026).

[6]   J. Radhakrishnan, An entropy proof of Bregman’s theorem, J. Combin. Theory Ser. A 77 (1997), 161–164.

[7]   L. M. Brègman, Certain properties of nonnegative matrices and their permanents, Dokl. Akad. Nauk SSSR 211 (1973), 27–30.

[8]   G. P. Egorychev, The solution of van der Waerden’s problem for permanents, Adv. Math. 42 (1981), 299–305.

[9]   D. I. Falikman, Proof of the van der Waerden conjecture regarding the permanent of a doubly stochastic matrix, Mat. Zametki 29 (1981), 931–938.

[10]   M. Kwan, A. Sah, M. Sawhney, Large deviations in random Latin squares, Bull. Lond. Math. Soc. 54 (2022), 1420–1438; arXiv:2106.11932.

[11]   M. Laurent, A. Schrijver, On Leonid Gurvits’s proof for permanents, Amer. Math. Monthly 117 (2010), 903–911.

[12]   N. Alon, S. Friedland, The maximum number of perfect matchings in graphs with a given degree sequence, Electron. J. Combin. 15 (2008), Note 13.

[13]   A. Ferber, V. Jain, B. Sudakov, Number of 1  -factorizations of regular high-degree graphs, Combinatorica 40 (2020), 315–344; arXiv:1803.10360.

[14] T. F. Bloom, Erdős Problem #725, https://www.erdosproblems.com/725.

[15] C. D. Godsil, B. D. McKay, Asymptotic enumeration of Latin rectangles, J. Combin. Theory Ser. B 48 (1990), 19–44.

[16] E. Li, The Godsil–McKay asymptotic for Latin rectangles in the sublinear range of Erdős Problem 725, arXiv:2608.01671 (2026).