Solving the n-Fractions Puzzle Using Integer Programming
Abstract
We present a teaching case study in which we solve the n-fractions puzzle using a Mixed Integer Programming model. The n-fractions puzzle is problem 41 in CSPLib, a library of test problems for Constraint Programming solvers. We first propose a model which is correct assuming exact arithmetic and show how to modify the model to account for floating point rounding errors. We then reuse ideas from constraint programming approaches to the puzzle and limit the lowest common multiple (LCM) of the denominators in the solution. This massively reduces solution time and means we can finally close the remaining open case to the puzzle. The apparent simplicity of the puzzle, the novelty of the modelling techniques used and the speedups achieved through limiting the LCM make the puzzle an interesting teaching case study.
Funding: This work was supported by the Australian Government (RTP Scholarship).
1. Introduction
Teaching advanced integer programming modelling and solution techniques to intermediate and advanced undergraduate students requires presenting a wide range of models and introducing a wide range of modelling techniques. One engaging way to do this is to use puzzles. In this paper we consider the n-fractions puzzle and show how it can be used to introduce several interesting and useful modelling techniques.
The original 3-fractions puzzle is specified as: find nine distinct nonzero digits that satisfy
This generalises to: find 3n nonzero digits satisfying
The n-fractions puzzle was presented as a puzzle in this journal (Malapert and Provillard 2018) and modelled as a constraint satisfactions problem (CSP). A CSP consists of a set of variables defined by their domains and a set of constraints. A constraint is a relation between a subset of variables that restricts the possible values that variables can simultaneously take. CSPs are usually solved by Constraint Programming (CP) engines such as Google’s OR-Tools (Google 2024), but can often be reframed as Mixed Integer Programming (MIP) problems and solved with a specialist MIP solver such as Gurobi (2024). The choice of which solver to use is often not clear and can be dependent on the modeller’s ability to express the problem in the best possible way for the environment in question.
The first appearance of the 3-fractions puzzle in the constraint satisfaction literature appears to be in Schulte and Smolka (2004). The n-fractions puzzle (Frisch et al. 2018) is problem 41 of CSPLib (Jefferson et al. 1999), a library of CSPs.
To the best of our knowledge, all the models proposed to date use CP to solve this puzzle. In Codish (2018) and Malapert and Provillard (2018) Equation (2) is restated as
Malapert and Provillard (2018) prove that the n-fractions puzzle has no solutions for . CSPLib lists solutions for all n from 3 to 43. Many of these are from Codish (2018) and Malapert and Provillard (2018) which report run times in excess of 100 hours for the larger instances.
This paper presents the puzzle as a flexible teaching case. The teaching case aims to guide students through a rich exploration of model reformulation in the classroom. Specifically, we present an integer programming model for the puzzle, and show how to modify the solution procedure to handle the limitations of floating point arithmetic. We then incorporate bounds on the LCM into the model, producing solutions for all n from 3 to 44 with solution times mostly under 1 second and a maximum of just 4 seconds. Further, we extend the case beyond simply solving problems by using LCM as the objective function, and find solutions with the smallest possible LCM for each n, as well as verifying that many solutions exist for all n from 4 to 44. The reformulation framework also provides students with a good exercise in approaching a single problem from multiple mathematical modelling perspectives.
The novel modelling techniques used to represent the fractions, cut off approximate solutions and calculate the LCM make this an ideal teaching example for intermediate to advanced students.
This short paper is organized as follows. In Section 2 we introduce the initial model and the LCM model. In Section 3 we describe our experience with using this puzzle as a teaching example. In Section 4 we detail our computation results before ending with a few concluding remarks.
2. The Integer Programming Model
2.1. Initial Model
Let and . We define integer variables which represent the number of times appears in (2). We additionally define as a function that counts the number of times i appears in the tuple t. For example, we write to count how many of i,j,k are equal to and which is 1 if .
We then formulate our MIP, a feasibility problem, as:
These directly encode the constraints of the problem.
This model quickly solves the n-fractions puzzle for small n. Additionally, it quickly returns infeasible for . However, if we use exact arithmetic to check (4), then for values of most of the solutions found do not satisfy the constraint. This is because the MIP solver uses floating point approximations for rational valued coefficients leading to the accumulation of small rounding errors.
This issue can be corrected by additional cuts, implemented as lazy constraints. Whenever we find a candidate solution we verify constraint (4) using exact arithmetic, with python Fractions library (Python Software Foundation 2024). If the constraint does not hold exactly, we add the additional constraint
With this constraint added we can find solutions for n up to 12 in a few seconds. However, a better approach is required to scale the model to larger n.
2.2. The LCM Model
Following the example of Codish (2018), we consider restricting the LCM of the fractions used. To do this, we need to calculate the LCM using only linear constraints. Let P be the set of prime numbers smaller than 100. We limit the LCM of any n-fraction to no more than some limit with the constraint
Additionally, we can introduce binary variables which are 1 if appears in the denominator of any fraction used in a solution and define to be the power of p in the prime factorisation of . In addition to the original model (4) to (8), the constraints required to limit the LCM are:
Constraints (11) force to be 1 if the denominator is used. Constraints (12) set the values of to the maximum power of p in any denominator that is used. Finally (13) limits the LCM. We initially set . If no solution is obtained for a given value of , we multiply it by 10 and try again until a solution is found.
3. Classroom Experience
3.1. Context
The n-fractions puzzle is used by the authors for a two-hour tutorial class in an advanced integer programming course. The class size is usually around 30 to 40 students with a mix of third- and fourth-year undergraduate students and masters students from various programs including a Bachelor of Mathematics and a Masters of Data Analytics. The advanced integer programming course is heavily advertised in an introductory course, so the students have chosen this course in the expectation that they will encounter challenging models. By the time the n-fractions puzzle is introduced they have already done some work with logic-based Benders decomposition and models with composite variables—variables which represent several underlying decisions. Students all have laptops with Gurobi installed. As the required code is relatively short, they can code along and try their own ideas.
Malapert and Provillard (2018) also report their teaching experience with the n-fractions puzzle. Their context shared some similarities in the level of students and size of the class, but was also different as they were teaching computer science students. They introduced the problem in one session, the students did some modelling work as homework and perhaps encountered floating point errors with (4). In the next session these problems were explored in more detail and LCM based approaches introduced.
Clearly the appeal of the puzzle applies in different contexts. It is simple to explain, rich in detail, with the possibility of many eye-opening moments for students.
3.2. Challenges
In our experience, the first difficulty students face is identifying the correct variables. A common initial idea is to have variables to determine the digits used in each position in the ith fraction. At this stage we point out that such a formulation is very symmetrical and, more importantly, that it is not possible to express constraint (2) linearly. This usually results in some students correctly identifying the variables used in this paper. A common modelling inefficiency here is to make the variables binary with another index for each of the n fractions. Repeating the emphasis on symmetry helps students understand that integer variables can be used.
Once the problem is solved for small values of n we get the students to use a suitable exact arithmetic tool to calculate the left-hand side of (2). For some this is the first exposure to floating point approximation issues. Students often try to tighten the floating point tolerances of Gurobi, but quickly realise something more is required. Their experience with logic-based Benders decomposition means some identify the need to use lazy constraints.
At this stage the problem is still difficult to solve, so we introduce the notion of a restricted LCM range from Codish (2018). It is still far from obvious how to calculate the LCM using linear constraints. Through a series of questions, we obtain a definition of the LCM corresponding to the left-hand side of (10). Due to the relative maturity of the students, this is often enough.
When teaching a new problem in class, the authors find it useful to get the students to explore similar problems, with slightly modified constraints or objectives. In this case, the students made several suggestions that were explored by the students in small groups. These suggestions and the results of the calculations are discussed in the computational results.
3.3. Student Response
This tutorial is regularly one of the most animated of the course. The students who self-select for this course enjoy puzzles and enjoy having to combine two techniques that they have previously used: composite variables and logic-based Benders decomposition.
Additionally, they are exposed to the new idea of restricting the search space, or using an objective as described in our Computational Results, in an attempt to find feasible solutions to a difficult integer programming problem. The authors provide examples from other domains where this may be useful—for example, a vehicle routing problem where the primary concern is to find a feasible solution with a fixed number of vehicles and minimising the total distance travelled helps the search process.
4. Computational Results
The models were implemented in Python and solved using Gurobi 11.0 on an x64 based PC equipped with 64 GB of RAM and an Intel i9 processor running at 2500 MHz. This processor has 14 cores and 20 logical processors but was limited to 8 threads when running the model. We set Gurobi’s branching direction parameter (BranchDir) to 1, as preliminary tests indicated that this helped find solutions. All runs were restricted to a run time limit of five minutes.
In our first test we ran the initial model, without lazy constraints. This produced solutions quickly, but when checked with exact arithmetic the solutions were invalid except for . The model also returns infeasible, in around 0.01 seconds, for .
We next ran the initial model with lazy constraints for . For solutions were found in a few seconds, and for a solution was found in 20 seconds. For other n no solution was found in the time limit, though several hundred approximate solutions were cut off by lazy constraints and several million branch and bound nodes were explored.
The LCM model produced outstanding results. Solutions were found for all , with the largest run time being around 4 seconds for . The LCM bound needed to increase from 1,000 to 10,000 for and all the way to 1,000,000 for .
We separately modified the model to remove constraint (13) and instead use the left-hand side of this equation as the objective in a minimisation problem. This gives the minimum LCM solution for each n. Table 1 lists n, the run times in seconds T, the number of constraints (9) added to cut off solutions that did not satisfy constraint (4) when calculated with exact arithmetic, the best LCM and the fractions in the solution found in each case. It is striking how small most LCMs are and how few cuts were required due to low values of the LCM.
|
Table 1. Solutions with Smallest LCM
| n | T | Cuts | LCM | Fractions |
|---|---|---|---|---|
| 3 | 0.27 | 0 | 204 | |
| 4 | 0.71 | 0 | 54 | |
| 5 | 0.43 | 0 | 54 | |
| 6 | 1.16 | 0 | 72 | |
| 7 | 0.72 | 1 | 54 | |
| 8 | 0.69 | 0 | 68 | |
| 9 | 0.77 | 0 | 76 | |
| 10 | 0.41 | 0 | 56 | |
| 11 | 0.58 | 0 | 76 | |
| 12 | 0.75 | 0 | 78 | |
| 13 | 0.63 | 0 | 76 | |
| 14 | 0.56 | 0 | 78 | |
| 15 | 0.59 | 0 | 156 | |
| 16 | 0.47 | 0 | 78 | |
| 17 | 0.76 | 0 | 96 | |
| 18 | 0.83 | 0 | 288 | |
| 19 | 0.71 | 0 | 96 | |
| 20 | 0.47 | 0 | 288 | |
| 21 | 0.72 | 0 | 288 | |
| 22 | 0.52 | 0 | 288 | |
| 23 | 0.47 | 0 | 288 | |
| 24 | 1.07 | 1 | 288 | |
| 25 | 0.53 | 0 | 288 | |
| 26 | 0.76 | 0 | 288 | |
| 27 | 0.54 | 0 | 380 | |
| 28 | 0.37 | 0 | 288 | |
| 29 | 0.53 | 0 | 380 | |
| 30 | 0.5 | 0 | 380 | |
| 31 | 0.41 | 0 | 380 | |
| 32 | 0.34 | 0 | 380 | |
| 33 | 1.26 | 0 | 1,824 | |
| 34 | 0.45 | 0 | 380 | |
| 35 | 1.47 | 0 | 2,400 | |
| 36 | 0.77 | 0 | 2,400 | |
| 37 | 0.83 | 0 | 2,400 | |
| 38 | 0.94 | 0 | 2,400 | |
| 39 | 2.42 | 10 | 7,980 | |
| 40 | 0.47 | 0 | 5,544 | |
| 41 | 1.28 | 3 | 7,980 | |
| 42 | 0.32 | 0 | 7,980 | |
| 43 | 0.47 | 0 | 7,980 | |
| 44 | 2.02 | 4 | 319,200 |
To explore the number of solutions we modified the call-back to store any solution found and add a constraint of the form (9) to cut off the solution even though it has been verified with exact arithmetic. In this way we can search for many solutions. For there appear to be very many solutions. Even for this procedure found 16 different solutions in five minutes, while also eliminating 1,969 approximate solutions and searching 1.6 million branch and bound nodes. Solutions are different in the sense that each solution uses at least one fraction that does not appear in any of the other solutions.
We (the class and instructors) were curious as to whether a slightly restricted form of the problem also has solutions. We modified the problem slightly so that the right-hand side of constraint (6) was set to . The model still found solutions for all n and ran slightly faster, taking around 1 second for .
Finally, we considered another modification to the problem where the fractions sum to . This problem is found to be infeasible for (no candidate solutions are found) and solutions are found for all other . All solutions had LCM under 1,000 and all problems took a fraction of a second to solve.
5. Conclusions
To date, all proposed approaches to solve the n-fractions puzzle have used constraint programming. Advances in solution performance have come from two fronts: the increased speed of solvers and hardware and reformulations which focus on that part of the search space with a low LCM. Here we propose an Integer Programming approach to the puzzle which also relies on the speed of modern IP solvers and hardware and focusing the search on low LCMs. Moreover, we introduce three novelties to the puzzle: integer variables to represent how many times a candidate fraction is used; lazy constraints to ensure the fractions add to 1 using exact arithmetic; and a linearisation, using logarithms, to calculate the LCM. It is likely that this model would have completed the puzzle even on the solvers and hardware available in 2009.
As a teaching case this puzzle exposes intermediate to advanced students to several useful concepts. These include: using variables that represent an aggregate quantity (the whole fraction in this case) in order to reduce symmetry; being wary of floating point rounding errors; adding lazy constraints to cut off solutions which are unacceptable for some reason; taking the log of both sides to reduce products to linear constraints; and using possibly superfluous constraints to restrict the solver to interesting parts of the search space. Our teaching experience with the case means we now use it every year.
References
- (2018) A SAT encoding for the n-fractions problem. Preprint, submitted July 2, https://arxiv.org/abs/1807.00507.Google Scholar
- (2018) CSPLib problem 041: The n-fractions puzzle. Accessed June 8, 2026, http://www.csplib.org/Problems/prob041.Google Scholar
Google (2024) Google OR-Tools reference. developers.google.com/optimization.Google ScholarGurobi (2024) Gurobi optimizer reference manual. http://www.gurobi.com/documentation/current/refman/index.html.Google Scholar- (1999) CSPLib: A problem library for constraints. http://www.csplib.org.Google Scholar
- (2018) Puzzle—Solving the n-fractions puzzle as a constraint programming problem. INFORMS Trans. Ed. 19(1):1–55.Google Scholar
Python Software Foundation (2024) Python fractions. docs.python.org/3/library/fractions.html.Google Scholar- (2004) Finite domain constraint programming in Oz: A tutorial. http://mozart2.org/mozart-v1/doc-1.4.0/fdt/node31.html#section.propagators.fractions.Google Scholar

