Isidor SelectionCriteria before scores

Puzzle & Logic

Which Lights Out boards can be solved? Counting with linear algebra from 1x1 to 15x15

On a 5x5 Lights Out board only one random start in four can be solved, and on 4x4 only one in sixteen. A table of the fraction for every size up to 15x15.

Lights Out is a puzzle on a square grid of lit and unlit cells. Pressing a cell toggles it and the cells directly above, below, left and right of it. The goal is to turn every light off. It looks like a game where any starting pattern can be cleared with enough thought. It is not, and the proportion that cannot be cleared is large enough to matter for anyone who builds or judges such a game.

What is known

Wikipedia’s article on the game summarises the mathematics. The order of presses does not matter, and in a minimal solution no cell is pressed more than once, because pressing a cell twice cancels out. It reports that Marlow Anderson and Todd Feil used linear algebra in 1998 to prove that not every configuration is solvable, and that any solvable 5x5 board has exactly four winning sets of presses. The board can be written as a column of 25 values, each 0 or 1, in arithmetic modulo 2, and a configuration is solvable only if it is orthogonal to two particular vectors.

We did not read the 1998 paper. We took those statements from the Wikipedia page and used them as a check on our own computation.

The idea in one paragraph

Every press is a vector of 0s and 1s showing which cells it flips. A set of presses is the sum of those vectors, with 1+1=0. The boards you can reach from a dark board are all the sums of press vectors: the span of those vectors. How big the span is depends on the rank of the press matrix. If its rank is r and the board has n x n cells, the solvable boards number 2^r out of 2^(n x n), and each solvable board has 2^(n x n - r) different solutions. The difference n x n - r is the nullity.

The table

We built the press matrix for every size from 1x1 to 15x15, computed its rank over the two-element field by Gaussian elimination, and read off the nullity.

Board Cells Rank Nullity Fraction of random boards that are solvable Solutions per solvable board
1x1 to 3x3 1, 4, 9 full 0 all 1
4x4 16 12 4 1/16 16
5x5 25 23 2 1/4 4
6x6 to 8x8 36 to 64 full 0 all 1
9x9 81 73 8 1/256 256
10x10 100 full 0 all 1
11x11 121 115 6 1/64 64
12x12, 13x13 144, 169 full 0 all 1
14x14 196 192 4 1/16 16
15x15 225 full 0 all 1

The 5x5 row agrees with Wikipedia: nullity 2 means two orthogonality conditions and 2^2 = 4 solutions per solvable board. The totals are 2^25 = 33,554,432 boards, of which 2^23 = 8,388,608 are solvable and 25,165,824 (75.0%) are not.

For two small sizes we also tried every possible set of presses. On 3x3 they produce all 512 boards. On 4x4 they produce 4,096 of the 65,536 boards, which is 6.25%, matching the rank computation.

One more check: in our computation the board with every light on is solvable at every size from 1x1 to 15x15. We did not look for a proof.

Why this matters

A random board is usually the wrong generator at some sizes. On 4x4, a random pattern is unsolvable 93.75% of the time. On 5x5 it is 75.0%. On 9x9, 255 of every 256. The sizes with full rank are different: on 6x6 and 10x10, every board is solvable and a random start is fine.

The safe way to generate a puzzle is to start from a cleared board and press cells at random. Any board produced that way is reachable by definition, so it is solvable. This follows from the definitions above rather than from a source.

“Every puzzle is solvable” is a claim a judge can test. Ask how the boards are generated. If the answer is “random lights” on a 5x5 grid, three in four will be impossible. If the answer is “scrambled from solved”, the claim holds.

The number of solutions varies by size. A 5x5 puzzle has four ways to clear it. A 9x9 has 256. A hint system that says “the solution” is describing one of several.

What we have not shown

  • Our table holds for the standard game where a press toggles the cell and its four neighbours. Variants with different press shapes, wrapped edges or more than two states need their own matrices.
  • We did not test how any particular app generates its boards.
  • Rank and nullity are computed values. We checked them with brute force at two sizes and against the Wikipedia statements for 5x5, but not at every size.
  • We did not look for patterns in which sizes have nullity 0.

How we calculated

The script was run in Python 3.

# Lights Out: n x n board, pressing a cell toggles itself and orthogonal neighbours. Solve over GF(2).
def rank_gf2(rows,ncols):
    rows=rows[:]; r=0
    for c in range(ncols):
        piv=None
        for i in range(r,len(rows)):
            if rows[i]>>c&1: piv=i;break
        if piv is None: continue
        rows[r],rows[piv]=rows[piv],rows[r]
        for i in range(len(rows)):
            if i!=r and rows[i]>>c&1: rows[i]^=rows[r]
        r+=1
    return r
def press_rows(n):
    rows=[]
    for i in range(n):
        for j in range(n):
            v=0
            for di,dj in ((0,0),(1,0),(-1,0),(0,1),(0,-1)):
                a,b=i+di,j+dj
                if 0<=a<n and 0<=b<n: v|=1<<(a*n+b)
            rows.append(v)
    return rows
print("n cells rank nullity solvable_fraction solutions_per_solvable")
for n in range(1,16):
    N=n*n; r=rank_gf2(press_rows(n),N); d=N-r
    print(n,N,r,d,"1/%d"%(2**d),2**d)
# brute-force check: all 2^(n*n) press subsets for n=4 and n=3, count distinct resulting boards
for n in (3,4):
    P=press_rows(n); N=n*n; seen=set()
    for mask in range(1<<N):
        b=0
        for k in range(N):
            if mask>>k&1: b^=P[k]
        seen.add(b)
    print("brute force n=%d distinct reachable boards %d of %d"%(n,len(seen),1<<N))
print("5x5 total",2**25,"solvable",2**23,"unsolvable",2**25-2**23,"pct unsolvable",(2**25-2**23)/2**25*100)
print("4x4 total",2**16,"solvable",2**12,"pct solvable %.4f"%(2**12/2**16*100))

# is the all-lights-on board in the span of the press vectors?
print("n: all-on board solvable?")
res=[]
for n in range(1,16):
    N=n*n; P=press_rows(n); allon=(1<<N)-1
    res.append((n, rank_gf2(P+[allon],N)==rank_gf2(P,N)))
print(res)

Output:

n cells rank nullity solvable_fraction solutions_per_solvable
1 1 1 0 1/1 1
2 4 4 0 1/1 1
3 9 9 0 1/1 1
4 16 12 4 1/16 16
5 25 23 2 1/4 4
6 36 36 0 1/1 1
7 49 49 0 1/1 1
8 64 64 0 1/1 1
9 81 73 8 1/256 256
10 100 100 0 1/1 1
11 121 115 6 1/64 64
12 144 144 0 1/1 1
13 169 169 0 1/1 1
14 196 192 4 1/16 16
15 225 225 0 1/1 1
brute force n=3 distinct reachable boards 512 of 512
brute force n=4 distinct reachable boards 4096 of 65536
5x5 total 33554432 solvable 8388608 unsolvable 25165824 pct unsolvable 75.0
4x4 total 65536 solvable 4096 pct solvable 6.2500
n: all-on board solvable?
[(1, True), (2, True), (3, True), (4, True), (5, True), (6, True), (7, True), (8, True), (9, True), (10, True), (11, True), (12, True), (13, True), (14, True), (15, True)]

Sources

Written for: Người đánh giá game giải đố muốn biết bàn Lights Out nào giải được, vì sao bàn ngẫu nhiên thường vô nghiệm, và cách kiểm tra lời hứa mọi màn đều giải được.

AI assistance recorded as: AI draft, not edited. Review status: approved.