Skip to content
Vamshi Jandhyala

Books · Solving Puzzles through Mathematical Programming

Chapter 45

The Fractions Puzzle

Nine different non-zero digits go in the nine boxes of ABC+DEF+GHI  =  1,\frac{A}{BC} + \frac{D}{EF} + \frac{G}{HI} \;=\; 1, where BCBC is shorthand for the two-digit number 10B+C10B + C, and EFEF and HIHI likewise. Each of the digits 11 through 99 is used once. There is, up to the order of the three fractions, a single solution, 912+534+768  =  1,\frac{9}{12} + \frac{5}{34} + \frac{7}{68} \;=\; 1, and the eye can confirm it: 9/129/12 is three-quarters, and 5/34+7/68=10/68+7/68=17/685/34 + 7/68 = 10/68 + 7/68 = 17/68, the remaining quarter. This is problem forty-one of the CSPLib, the standard library of constraint benchmarks, and it generalises in a way that turns a one-line riddle into a question still partly open.

From three fractions to nn

Replace three fractions by nn and ask for 3n3n non-zero digits with i=1nxi10yi+zi  =  1.\sum_{i=1}^{n} \frac{x_i}{10 y_i + z_i} \;=\; 1. Each digit from 11 to 99 may now be reused, between once and n/3\lceil n/3 \rceil times, so that the stock of nine digits keeps pace with the 3n3n boxes. At n=3n = 3 the ceiling is one, every digit appears exactly once, and this is the original puzzle. Because each fraction is at least 1/991/99, no more than 9999 of them can sum to 11, so the family is finite. The question is the largest nn for which the digits can be found.

What makes it hard is not the combinatorics but the arithmetic. The obvious program reads the equation as written and adds the fractions in floating-point, and from about n=6n = 6 rounding error starts to certify sums that are not really 11. The obvious repair, clearing denominators by multiplying through by the product i(10yi+zi)\prod_i (10 y_i + z_i), replaces rounding error with overflow: that product passes a 3232-bit integer at n=6n = 6 and a 6464-bit one not long after. The puzzle is a small monument to the gap between a model that is correct on paper and one that is correct in a machine.

Clearing the denominators

The cure is to clear the denominators with their least common multiple rather than their product. The two agree only when the denominators are coprime; in every solution they share factors heavily, and the least common multiple stays small where the product explodes. The whole model is built around one extra variable that carries it.

Variables

For each fraction i{1,,n}i \in \{1, \ldots, n\} three digit variables give its numerator and the two figures of its denominator, xi,yi,zi{1,,9},di=10yi+zi{11,,99}.x_i,\, y_i,\, z_i \in \{1, \ldots, 9\}, \qquad d_i = 10 y_i + z_i \in \{11, \ldots, 99\}.

One linear equation

The constraint ixi/di=1\sum_i x_i / d_i = 1 divides by unknown denominators, which no integer solver will accept. Clear them all at once. Let LL be a positive integer that every did_i divides, and let qi=L/diq_i = L / d_i be the corresponding cofactor. Then xi/di=xiqi/Lx_i / d_i = x_i q_i / L, and the sum-to-one condition becomes a single linear equation, i=1nxiqi  =  L.\sum_{i=1}^{n} x_i q_i \;=\; L . The cofactors and the divisibility are pinned by one family of product relations, L=diqi(i=1,,n),L = d_i\, q_i \qquad (i = 1, \ldots, n), which simultaneously force each did_i to divide LL and define qi=L/diq_i = L / d_i. Any common multiple LL would serve, since the equation is unchanged by scaling LL and every qiq_i together; minimising LL picks out the least common multiple, the smallest honest common denominator, and keeps the numbers as small as the puzzle allows. CP-SAT carries the handful of products L=diqiL = d_i q_i and pi=xiqip_i = x_i q_i directly, which is all the model needs beyond linear constraints.

Counting and symmetry

Two families of constraints remain. The counting rule asks that each digit value vv be taken by between one and n/3\lceil n/3 \rceil of the 3n3n digit variables. And the nn fractions may be permuted freely, a symmetry that multiplies every solution by n!n! copies; ordering the fractions by value removes it. Since xi/di=xiqi/L=pi/Lx_i / d_i = x_i q_i / L = p_i / L with a common LL, ordering by value is just p1p2pnp_1 \le p_2 \le \cdots \le p_n on the scaled numerators the model already has.

A solver

from ortools.sat.python import cp_model
from math import ceil

def n_fractions(n, lmax=200_000):
    m = cp_model.CpModel()
    x = [m.NewIntVar(1, 9, "") for _ in range(n)]  # numerators
    y = [m.NewIntVar(1, 9, "") for _ in range(n)]  # denom. tens
    z = [m.NewIntVar(1, 9, "") for _ in range(n)]  # denom. ones
    d = [m.NewIntVar(11, 99, "") for _ in range(n)]
    q = [m.NewIntVar(1, lmax, "") for _ in range(n)]
    p = [m.NewIntVar(1, lmax, "") for _ in range(n)]
    L = m.NewIntVar(1, lmax, "")
    for i in range(n):
        m.Add(d[i] == 10 * y[i] + z[i])
        m.AddMultiplicationEquality(L, d[i], q[i])
        m.AddMultiplicationEquality(p[i], x[i], q[i])
    m.Add(sum(p) == L)             # the sum equals 1
    for v in range(1, 10):         # digit v: 1..ceil(n/3)
        used = [m.NewBoolVar("") for _ in range(3 * n)]
        for b, g in zip(used, x + y + z):
            m.Add(g == v).OnlyEnforceIf(b)
            m.Add(g != v).OnlyEnforceIf(b.Not())
        m.AddLinearConstraint(sum(used), 1, ceil(n / 3))
    for i in range(n - 1):
        m.Add(p[i] <= p[i + 1])    # order fractions (symmetry)
    m.Minimize(L)                  # least common denominator
    s = cp_model.CpSolver()
    if s.Solve(m) not in (cp_model.OPTIMAL, cp_model.FEASIBLE):
        return None
    val = s.Value
    return [(val(x[i]), val(y[i]), val(z[i])) for i in range(n)]

For n=3n = 3 the model returns the three fractions 9/129/12, 5/345/34, 7/687/68 and nothing else: with the ceiling equal to one the counting rule forces all nine digits distinct, the symmetry order fixes their arrangement, and the common denominator comes out at L=204=lcm(12,34,68)L = 204 = \operatorname{lcm}(12, 34, 68), against which 917+56+71=2049 \cdot 17 + 5 \cdot 6 + 7 \cdot 1 = 204 confirms the sum. The same code runs on without change. It finds twelve fractions inside a common denominator of only 7878, a vivid measure of how far the least common multiple lags the product, which by then would have hundreds of digits. Past the middle teens the search rather than the size is what slows, and reaching the frontier needs the sharper machinery below.

Where it stops

Two results bracket the answer. From above, the puzzle is impossible once nn is large enough, and the reason is a counting bound. To make the sum as small as it can be, put the smallest digits on top and the largest underneath, with each denominator’s tens figure no less than its ones figure (a larger denominator where it costs nothing). Even this most favourable arrangement has a sum that grows with nn, and once it passes 11 the equation cannot hold. Writing n=3k+rn = 3k + r, the minimal sum is at least (176+286+396)k(r=0),(177+287+397)k+(r=1,2),\Bigl(\tfrac{1}{76} + \tfrac{2}{86} + \tfrac{3}{96}\Bigr) k \quad (r = 0), \qquad \Bigl(\tfrac{1}{77} + \tfrac{2}{87} + \tfrac{3}{97}\Bigr) k + \cdots \quad (r = 1, 2), and these cross 11 at n=45n = 45, 4646, and 4747 respectively, so the bound clears 11 for every n45n \ge 45. The fractions puzzle therefore has no solution from n=45n = 45 on.

From below, a constraint search closes almost everything beneath that ceiling. Solutions exist for every nn from 33 to 3535 and for 3737, 3838, and 4040; the largest fractions puzzle anyone has solved is the 4040-fraction one. Six cases, n{36,39,41,42,43,44}n \in \{36, 39, 41, 42, 43, 44\}, were neither solved nor ruled out and remain open, the multiples of three among them resisting longest because their tighter counting rule, every digit used exactly kk times, leaves the least room to manoeuvre. The gap between the largest solved puzzle and the proven ceiling is those six numbers, and closing it, finding a fraction or proving there is none, is the puzzle’s standing open problem.

Sources. The fractions puzzle is problem 041041 of the CSPLib, the constraint-satisfaction benchmark library, contributed by Alan Frisch, Christopher Jefferson, Ian Miguel, and Toby Walsh in 19991999; its nn-fraction generalisation, the counting rule, and the symmetry-breaking orders trace through Christian Schulte and Gert Smolka and through Alan Frisch and collaborators’ studies of implied constraints. The least-common- multiple model used here, the proof that the puzzle is unsatisfiable for n45n \ge 45, and the search that solves it up to 4040 fractions while leaving n{36,39,41,42,43,44}n \in \{36, 39, 41, 42, 43, 44\} open are due to Arnaud Malapert and Julien Provillard, Puzzle—Solving the nn-Fractions Puzzle as a Constraint Programming Problem, INFORMS Transactions on Education volume 1919, number 11 (20182018), pages 48485555, whose models reach the same frontier with a precomputed prime factorisation of each denominator in place of the bounded common multiple taken here. The unique three-fraction solution and the twelve-fraction instance are produced by the model above and re-checked in exact rational arithmetic.