Egyptian Fraction asks you to write any proper fraction as a sum of distinct unit fractions — fractions whose numerator is 1, like 1/3 or 1/11. The ancient Egyptians wrote every fraction this way, and a simple greedy rule always works.
Problem. Given a proper fraction n/d (with 0 < n < d), express it as a sum of distinct unit
fractions 1/x1 + 1/x2 + ..., where each xi is a positive integer. Return the list of denominators.
Example: n/d = 6/14 (which reduces to 3/7) -> 1/3 + 1/11 + 1/231.
The slow way first
You might try to search for a set of denominators that add up exactly to n/d — testing combinations of 1/2, 1/3, 1/4, ... until one set works. That search blows up fast: there are exponentially many subsets to try, and no obvious way to prune them.
The question to ask: what is the single largest unit fraction I can subtract right now without going negative? If I always take the biggest legal piece, the remainder shrinks every step — and it turns out it shrinks fast enough to terminate.
The idea: take the biggest unit fraction that fits
For n/d, the largest unit fraction 1/x that is not larger than n/d uses x = ceil(d / n). Subtract it. The new fraction is:
n/d - 1/x = (n*x - d) / (d*x)
so we update n to n*x - d and d to d*x, then repeat. Each step the new numerator n*x - d is strictly smaller than the old n, so n keeps dropping and eventually hits 0 — at which point we are done.
Because we always grab the largest legal piece, the denominators come out strictly increasing, so the unit fractions are automatically distinct.
Walk through it
Step through the animation starting from 3/7. We pick 1/3 (since ceil(7/3) = 3), leaving 2/21. Then 1/11 (since ceil(21/2) = 11), leaving 1/231. That last fraction already has numerator 1, so it is itself a unit fraction. The result list [1/3, 1/11, 1/231] builds up underneath.
Pseudocode
result = empty list
while n is not 0:
x = ceil(d / n) # smallest x so 1/x <= n/d
append x to result
n = n*x - d # numerator of the remainder
d = d*x # denominator of the remainder
return result # denominators of the unit fractionsThe Python solution
def egyptian_fraction(n, d):
result = []
while n != 0:
x = -(-d // n) # ceil(d / n)
result.append(x)
n = n * x - d
d = d * x
return resultresultcollects the denominatorsxof each unit fraction1/x.- The loop runs until the numerator
nbecomes0— that is when nothing is left to express. -(-d // n)is the integer ceiling ofd / n(Python floor-divides toward negative infinity, so negating twice rounds up).n = n * x - dandd = d * xtogether replacen/dwith the remainder(n*x - d) / (d*x).- When
nreaches a numerator of1,x = dand the subtraction sendsnto0, ending the loop.
Complexity
| Case | Time | Notes |
|---|---|---|
| Subset search (brute force) | O(2^k) (moderate) | tries combinations of denominators |
| Greedy (this solution) | O(terms) (moderate) | one unit fraction per loop |
O(terms) (moderate)The greedy method produces a representation in just a handful of terms for typical inputs, and the numerator strictly decreases each step, so the loop is guaranteed to end. The space is whatever it takes to hold the output list.
When this pattern shows up
Greedy works here because a local choice (grab the biggest unit fraction) provably leaves a smaller, same-shaped subproblem. Whenever you can argue that the largest valid piece never hurts the remainder, reach for greedy instead of search.
Compute the ceiling carefully. Plain d // n floors, which would pick a unit fraction that is too big
and push the numerator negative. Use -(-d // n) (or math.ceil) so 1/x never exceeds n/d.
Practice
For n/d = 2/21, what unit fraction does the greedy step pick, and what fraction is left over?
1. What value of x does the greedy step choose for n/d?
2. After picking 1/x, what is the new numerator?
3. When does the loop stop?
4. Why are the resulting unit fractions distinct?