Skip to content

Instantly share code, notes, and snippets.

@apsicle
Created January 17, 2017 23:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apsicle/da3bc62eced4a2f585ca75b300e1972b to your computer and use it in GitHub Desktop.
Save apsicle/da3bc62eced4a2f585ca75b300e1972b to your computer and use it in GitHub Desktop.
from fractions import Fraction as f
import math
def decompose(n):
frac = f(n)
out = []
current_denom = 1
if frac >= 1:
number = int(frac)
frac -= number
out.append(str(number))
while frac > 0:
current_denom = math.ceil(1/float(frac))
candidate = f(1, current_denom)
assert candidate <= frac
frac -= candidate
out.append(str(candidate))
print(n, out)
return out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment