# PiAppx.py """ Estimates pi with a fraction. Inputs an integer numerator and determines the best denominator so that the ratio is as close to pi as possible. Showcases the floor function from the math module.""" import math num = input('Enter a positive integer greater than three: ') # There are two possible denominators and they # are the two closest integers to num/pi d1 = math.floor(num/math.pi) errD1 = abs(num/d1 - math.pi) d2 = d1+1 errD2 = abs(num/d2 - math.pi) if errD1 < errD2: print '\n| %1d/%1d - pi | = %8.3e' %(num,d1,errD1) else: print '\n| %1d/%1d - pi | = %8.3e' %(num,d2,errD2)