import sys # Ask Python for a large stack. sys.setrecursionlimit(100000) def pi_series_iter(n) : # Approximate (pi**2)/6 using n terms. Based on the formula: # (pi**2)/6 = 1 + 1/4 + 1/9 + 1/16 + 1/25 + ... result = 0 for i in range(1, n+1) : result = result + 1/(i**2) return result def pi_approx_iter(n) : x = pi_series_iter(n) return (6*x)**(.5) def pi_series_rec(n) : # As above, using recursion # Approximate (pi**2)/6 using n terms. Based on the formula: # (pi**2)/6 = 1 + 1/4 + 1/9 + 1/16 + 1/25 + ... # Base case if n == 0 : return 0 else : smaller_solution = pi_series_rec(n-1) solution = 1/n**2 + smaller_solution return solution def pi_approx_rec(n) : x = pi_series_rec(n) return (6*x)**(.5)