Suppose you have an n by m chessboard. How many ways can you cover the chessboard with dominoes?
It turns out there’s a remarkable closed-form solution:
Here are some questions you may have.
But what if n and m are both odd? You can’t tile such a board with dominoes.
Yes, in that case the formula evaluates to zero.
Do you need an absolute value somewhere? Or a floor or ceiling?
No. It looks like the double product could be a general complex number, but it’s real. In fact, it’s always the square of an integer.
Update: Apparently the formula does need an absolute value, not to turn complex values into real values but to turn negative integers into positive ones. See Aaron Meurer’s example below.
Does it work numerically?
Apparently so. If you evaluated the product in a package that could symbolically manipulate the cosines, the result would be exact. In floating point it cannot be, but at least in my experiments the result is correct when rounded to the nearest integer. For example, there are 12,988,816 ways to tile a standard 8 by 8 chessboard with dominoes, and the following python script returns 12988816.0. For sufficiently large arguments the result will not always round to the correct answer, but for moderate-sized arguments it should.
from numpy import pi, cos, sqrt def num_tilings(m, n): prod = 1 for k in range(1, m+1): for l in range(1, n+1): prod *= 2*cos(pi*k/(m+1)) + 2j*cos(pi*l/(n+1)) return sqrt(abs(prod)) print(num_tilings(8,8))
The code looks wrong. Shouldn’t the ranges go up to
No, Python ranges are half-open intervals.
range(a, b) goes from
b-1. That looks unnecessarily complicated, but it makes some things easier.
You said that there was no need for absolute values, but you code has one.
Yes, because while in theory the imaginary part will be exactly zero, in floating point arithmetic the imaginary part might be small but not zero.
Where did you find this formula?
Thirty-three Miniatures: Mathematical and Algorithmic Applications of Linear Algebra
5 thoughts on “How many ways can you tile a chessboard with dominoes?”
Wow, this is a great test for symbolic simplification. I already found two bugs in SymPy with it https://github.com/sympy/sympy/issues/11533.
I thought about trying SymPy on it. Glad you’re doing it instead since you’re probably 100x faster than I am.
If you remove the sqrt and abs from the formula, you get -9 for the 2 x 3 case. I checked this both numerically with numpy and symbolically with SymPy.
I think that for fixed k, the term for l=j and l = n+1-j are conjugate. So the product is real. Is there a closed form for just the product over l with fixed k?
I have bought the book. It seems to me that he draws the formula out of the air. I have seen a paper that goes a bit further back, but draws another formula out of the air.
All the papers seem to deal with adjacency matrices. The result shows much more symmetry than these.
In particular, it is clear why the formula gives a zero result for both m and n odd. m + 1 and n+1 are even so there is term cos(pi/2) +i cos(pi/2).
The formula in squares comes directly from the definition of the modulus of a complex number. The half range version seems plausible from considerations of the balance between opposite ends of the cos curve in the half circle range, at least for even m, n.