Calculates Pi to an increasing degree of accuracy using Leibniz formula. The more cycles that it runs for then the greater the accuracy of the result.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
num_cycles = raw_input("Please enter the number of cycles for estimating Pi: ") pi = 0.0 sign = 1 denom = 1.0 for i in range(0, int(num_cycles)): pi += sign/denom sign *= -1 denom += 2 pi *= 4.0 print "Pi approximation is: " + str(pi) |
Questions? Comments?