Recall from your textbook that the Leibniz formula states that \(\frac{\pi}{4} = \frac{1}{1} - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \cdots\)
The Wallis formula states that \(\frac{\pi}{2} = \frac{2}{1} \times \frac{2}{3} \times \frac{4}{3} \times \frac{4}{5} \times \frac{6}{5} \times \frac{6}{7} \times \frac{8}{7} \times \cdots\)
Create a file inifiniteSeries.py
and save it to cs100/ch2
.
Copy the following Python code into your program. It computes the Natural Log of 2, which is 0.69314718056, by summing fractions of alternating signs:
# This program computes the sum of the reciprocals
# of the integers with alternating signs. This
# series sums to the ln(2), or natural log of 2
# which is 0.69314718056
#
# Author: D. Palmer
#
# Date: Sept. 12, 2022
#
def sumAlternatingFractions(numTerms):
# initialize summation to be 0 the additive identity
sum = 0
# start the denominator at 1
denominator = 1
# start the sign as positive
sign = 1
#repeat until the specified number of terms have
# been summed
for iter in range(numTerms):
# compute the denominator value
fraction = 1 / denominator
# multiply it by the sign and sum
sum = sum + (sign*fraction)
# flip the sign to the alternate
sign = sign * -1
# compute new denominator by adding 1 to it
denominator = denominator + 1
print(" ", sum)
return sum
steps = 10
print(" ", sumAlternatingFractions(steps))
print("The natural log of 2: ", 0.69314718056)
Run this program for several different numbers of steps: 10, 50, 100, 250, 500, 1000, 2000 and see the progression towards the natural log of 2.
# This function computes the sums of the inverse of a base
# raised to successively higher powers
#
# sum = 1/1 + 1/n + 1/n**2 + 1/n**3 + 1/n**4 + ...
#
# This is a converging series that results in n/(n-1)
#
# Author: D. Palmer
#
# Date: Sept. 12, 2022
#
def sumInversePowers(numTerms, n):
sum = 0
denominator = 1
for iter in range(numTerms):
fraction = 1 / denominator
sum = sum + fraction
denominator = denominator * n
print(" ", sum)
return sum
steps = 10
base = 3
print(" ", sumInversePowers(steps,base))
print(">>> ", base/(base-1))
Run this program for many different values of steps and base. The document the program (in the style dome for the program above) to explain how this function produces the summation of the series.
Add and implement the functions which approximate Pi using the Leibniz and Wallis formulas. As you test your function, it will help for you to initially print out the terms (as a numerator, followed by a “/”, followed by the denominator) to check if you are calculating correcly. Remove those print statements once you confirm the terms are correct.
I suggest you call your Leibniz function for 200 and 500 terms, and the Wallis function for 4, 20, and 100 pairs. Here is a sketch of your program.
def leibniz(terms):
# your code goes here
approxPi = 0
return approxPi
def wallis(numPairs):
# your code goes here
approxPi = 1
return approxPi
print("Results for Leibniz formula:")
print(leibniz(200))
print(leibniz(500))
print("Results for Wallis formula:")
print(wallis(4))
print(wallis(20))
print(wallis(100))
Note: Type help('math')
in the Shell area. Use it (or the corresponding website)
Submit your file that should contain FOUR functions - two copied from here, and two written by you, to Moodle.