Note: This solution is overly complex, and one could wonder if the use of for loops is wise. It is important to familiarise ourselves with manipulating string to build the expected output. A more readable implementation of the function is given below:
Write a more generic function x_by_y_grid(rows, cols) that draws a similar grid with rows rows and cols columns.
Exercise 2: Srinivasa Ramanujan infinite series
Answer
You will notice that the solution has two functions. The second function compute_term(k) is a convenience function that computes a single term of the series. Convenience functions are used to make the code easier to read. As you can see, the function estimate_pi() needs to compute the term of a series in two places (line 11 and 15). Rather than duplicating the code (remember it is bad practice), I have created a function and called it twice.
import math
def estimate_pi():
"""return the apporximation of PI using Srinivasa Ramanujan's
infinite series.
Returns:
float: the apporximation of PI
"""
k = 0
term = compute_term(k)
inverse_pi = term
while term > 1e-15:
k = k + 1
term = compute_term(k)
inverse_pi += term
inverse_pi *= (2 * pow(2, 0.5)) / 9801
return 1 / inverse_pi
def compute_term(k):
"""compute a single term of the summation of Srinivasa Ramanujan
infinite series.
Args:
k (int): the index of the term's series
Returns:
float: the kth term of the summation
"""
output = (math.factorial(4*k)* (1103 + 26390 * k))
output /= pow(math.factorial(k), 4) * pow(396, 4*k)
return output
Note also that the index k is incremented at the start of the loop (line 14) rather than the end. This is to ensure that the term k=0 is not computed twice.
Finally, the code is documented via docstring. You can learn more about docstring and documenting your code in the chapter "Code documentation".
Write a function estimate_pi() that uses this formula to compute and return an estimate of π. It should use a while loop to compute terms of the summation until the last term is smaller than 1e-15 (which is Python notation for 10−15). You can check the result by comparing it to math.pi.