Skip to content

Instantly share code, notes, and snippets.

@Caleb2501
Last active December 17, 2015 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Caleb2501/5588869 to your computer and use it in GitHub Desktop.
Save Caleb2501/5588869 to your computer and use it in GitHub Desktop.
This one is an intermediate challenge. I had to have quite a bit of help on it. I also had to crte a break line that resets the fraction to 3/1 because it kept freezing up on me. well it is finished anyway. I also figured out that in order to get a float from two ints you have to divide them as floats. makes sense in hind sight.
#Happy (Be-Lated) Pi Day! To celebrate, write a program that calculates
#a list of rational approximations of Pi. Output should look like:
#3/1, 22/7, 333/106, 355/113, 52163/16604, 103993/33102, ...
import math
piLen = 0
def examineFract(num):
"""takes a float value in the form of a string and an int value for the
number of digits that pi should be displayed at."""
loop=True
num = float(num)
upperPI = (math.pi + .003)
lowerPI = (math.pi - .003)
y = 0
while loop:
if (3*num + y)/num > lowerPI and (3*num + y)/num < upperPI:
num = int(num)
num2 = 3*num + y
return num2, num
else:
y += 1
if y > 10:
y = 0
num += 1
if num > 1000:
print "Oh god what are you doing?!!"
return 3, 1
print "This program with gather the fractals of PI."
print "Please enter how many times you would like the scan to proceed"
iters = input(">> ")
start = 1
for i in range(1, (iters + 1)):
dividend, divisor = examineFract(start)
print "Result: %d/%d" %(dividend, divisor)
print "Decimal: ", (float(dividend) / float(divisor))
start = (divisor + 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment