CS 100

Logo

Scientific Computing

Activity 8 - Pi: Leibniz and Wallis approximations

Recall from your textbook that the Leibniz formula states that

The Wallis formula states that

  1. Open your pi.py file that you saved to CS100/ch2. Add and implement the functions which approximate Pi using the Leibniz and Wallis formulas. 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))
    
  2. Compare the approaches to compute Pi using Archimedes, Leibniz, and Wallis approximations. Which one is better for a comparable number of sides / terms / pairs ?

  3. Type help(math) in the Shell area. Use it (or the corresponding website) to determine how to print the constant Pi from the math library.

  4. What other functions from the math library can you use? Try a few to experiment! 🧮

If you finish early

  1. There is a famous summation, . Write a function called computeSummation(terms) that will compute the summation up to so many terms. Test it by invoking your function. What does the value approach for very large terms? (Make sure you get a decimal result).

How to submit

Submit your new (modified) pi.py file to Moodle.