Editor

Egyptian Fractions

#349 · Math · Advanced · 120 XP · about 20 min

Scribes in ancient Egypt wrote fractions as sums of different unit fractions, the fractions with 1 on top: 4/13 = 1/4 + 1/18 + 1/468.

A greedy rule always finds such a sum. Take the largest unit fraction that is not more than what is left, subtract it, and repeat until nothing is left. For 4/13 the largest unit fraction that fits is 1/4 (1/3 is too big), which leaves 3/52. Next comes 1/18, which leaves 1/468, and that is a unit fraction itself.

Write egyptian(num, den) that applies the greedy rule to the fraction num/den and returns the list of denominators in the order they were chosen.

Examples

egyptian(2, 3)     # returns [2, 6]
egyptian(4, 13)    # returns [4, 18, 468]
egyptian(1, 7)     # returns [7]

Constraints

  • 1 <= num < den <= 1,000
  • num/den may not be in lowest terms: 6/8 is 3/4
  • The denominators can grow to hundreds of digits, so finding each one by counting upwards will never finish.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.