CodyKochmann
11/7/2017 - 10:08 PM

iterate digits in a fractions decimal form in python

iterate digits in a fractions decimal form in python

from generators import loop

def iter_division(top, bottom):
    ''' iterate digits in a fraction's decimal form '''
    current, remainder = top/bottom, top%bottom
    # yield digits before the decimal
    for i in map(int, str(current)):
        yield i
    # yield the digits after the decimal
    for _ in loop():
        current, remainder = (remainder*10)/bottom, (remainder*10)%bottom
        yield current

pi = iter_division(22, 7)
for i in range(10):
    print(next(pi))