Algorithm to compute k fractions of form 1/r summing up to 1
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
This is a classic Egyptian-fraction problem: write 1 as a sum of exactly k unit fractions of the form 1 / r. The most important clarification is whether denominators may repeat, because the construction is different in the repeated and distinct cases.
Start With The Small Cases
If repetition is allowed, then some cases are immediate. For k = 2:
If denominators must be distinct, k = 2 has no positive-integer solution, but k = 3 does:
That identity is the usual base case for constructive proofs. Once you have one correct decomposition, the goal is to increase the number of terms without changing the sum.
The Key Splitting Identity
The standard trick is this identity:
You can verify it with a common denominator:
Each time you apply this identity, one term becomes two terms. The total sum stays the same, so the number of unit fractions increases by exactly one.
A Constructive Algorithm For Distinct Denominators
For distinct denominators, start from:
If you need more than three terms, repeatedly replace the current largest denominator n with:
This works because both new denominators are larger than n. If you always split the largest existing denominator, the new values will not collide with any earlier, smaller denominator, so distinctness is preserved.
For example, to get four terms, split 1 / 6:
To get five terms, split 1 / 42:
That already gives an inductive algorithm for every k >= 3.
Python Implementation
The construction maps naturally to code. Using exact fractions avoids floating-point mistakes when verifying the result.
If you allow repeated denominators, the case is even simpler. Since k * (1 / k) = 1, the list [k, k, ..., k] with k copies always works. The harder and more interesting version is the distinct case, which is why most discussions focus on the splitting identity.
Why The Algorithm Works
The proof is short and constructive:
- The base case
1 = 1/2 + 1/3 + 1/6is correct. - Each splitting step replaces one term
1/nwith two terms that sum to the same value. - Therefore the total sum stays equal to
1after every step. - If you split the largest denominator, the new denominators are larger and remain distinct from the earlier ones.
That is enough for an induction on the number of terms. Starting at three terms, you can build four, then five, and so on.
Complexity And Practical Notes
The algorithm is simple, but the denominators grow very quickly. After a few splits, values become large because of the multiplication by n(n + 1). For a proof or a contest problem, that is usually fine. For a production system that must print compact decompositions, you may want a different objective function, such as minimizing the largest denominator.
Still, for the specific task "produce any valid representation with exactly k unit fractions," this construction is hard to beat. It is deterministic, easy to prove correct, and easy to implement.
Why The Greedy Egyptian-Fraction Algorithm Is Different
The classic greedy Egyptian-fraction algorithm also writes rationals as sums of unit fractions, but it is designed to terminate with some decomposition, not to produce exactly k terms. If the requirement is "exactly k fractions," the splitting identity is the right tool because each application increases the count by one in a controlled way.
That direct control is the main reason this construction is taught for the exact-term-count version of the problem.
Common Pitfalls
- Forgetting to ask whether repeated denominators are allowed.
- Assuming
k = 2has a distinct positive solution when it does not. - Using the greedy Egyptian-fraction algorithm even though it does not control the final number of terms.
- Splitting arbitrary denominators and then accidentally creating duplicates.
- Verifying the result with floating-point arithmetic instead of exact rational arithmetic.
Summary
- The key identity is
1 / n = 1 / (n + 1) + 1 / (n(n + 1)). - If repeats are allowed,
kcopies of1 / kalways sum to1. - For distinct denominators,
1 = 1/2 + 1/3 + 1/6is the standard base case. - Repeatedly splitting the largest denominator gives a valid construction for every
k >= 3. - Exact arithmetic is the safest way to verify the output in code.
Related reading
- Algorithm to control acceleration until a position is reached
- Algorithm to convert an IEEE 754 double to a string?
- Algorithm to convert any positive integer to an RGB value
- Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both
- Algorithm to count occurrences of a matrix inside a larger one
- Algorithm to cover maximal number of points with one circle of given radius
- algorithm to define a geofence and see if a point is inside/outside it
- Algorithm to detect intersection of two rectangles?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.