Rodney Dunning's Home Page | Research and Scholarship | VPython | Approximating π


Introduction

The ratio of the circumference to the diameter is a constant for all circles. Traditionally represented by the Greek letter π, this number appears in a bewildering number of mathematical expressions, and has fascinating people for millennia. Indeed, the effort to simply calculate the value of π has produced many interesting results in number theory.

Description

The program provides two methods for approximating π, the Su-li Ching-yun algorithm and the Sharp-Gregory-Leibniz algorithm. The results are compared to value from the intrinsic function acos(-1.0).

When you execute the program, you will be prompted for the number of terms to use in the approximations.

Screen Shot

There is no visual component to this program. But here is a sample output stream, for which the user requested 500 terms in each approximation method:

>>> ================================ RESTART ================================
>>> 
This program approximates pi using two
different methods.  Results are compared to
acos(-1.0).

The more terms the program uses, the more accurate
the approximations.  However, more terms
also means the program takes longer to run.

Some approximations converge more rapidly than
others.  The number of terms you enter is used
for all the approximations, so you can compare
the approximations for convergence.

Enter the number of terms for the approximations: 50

Terms:  50
--------------------------------------
Su-li Ching-yun = 3.141592653589794
SGL             = 3.141592653589794
acos(-1.0)      = 3.141592653589793
ACTUAL          = 3.14159265358979323846
>>> 

Suggested Use

I haven't used this program in any courses. You can show it to students as an example program to teach programming skills. Consider having students research additional approximation methods and adding them to the code.

Hints:

Source Code

The source code appears between the bars below. Copy and paste the code into the Python IDLE environment, and hit F5 to run the code.


from math import *
from __future__ import division

"""
    Rodney Dunning
    Assistant Professor of Physics
    Longwood University

    This program approximates pi.
"""

print "This program approximates pi using two"
print "different methods.  Results are compared to"
print "acos(-1.0)."
print "" # spacer
print "The more terms the program uses, the more accurate"
print "the approximations.  However, more terms"
print "also means the program takes longer to run."
print "" # spacer
print "Some approximations converge more rapidly than"
print "others.  The number of terms you enter is used"
print "for all the approximations, so you can compare"
print "the approximations for convergence."
print "" # spacer
number_terms = input("Enter the number of terms for the approximations: ")

# Su-li Ching-yun formula
factor = 1
for i in xrange(1,number_terms):
    numerator = 1
    x = 1
    for j in xrange(i):
        numerator *= x**2
        x += 2
    denominator = 1
    y = 4
    for k in xrange(2*i):
        denominator *= y
        y += 2
    factor += (numerator / denominator)

su_li = 3*factor

# Sharp-Gregory-Leibniz
pre_factor = 2*sqrt(3)
sign = -1
power = 1
denominator = 1
x = 3
factor = 1
for i in xrange(1,number_terms):
    denominator *= sign * 3**(power) * x
    x+=2
    if(sign==1):
        sign=-1
    else:
        sign=1
    #accumulate
    factor += (1 / denominator)
    power += 1
    denominator = 1
SGL = pre_factor*factor

print "" # spacer
print "Terms: ",number_terms
print "--------------------------------------"
print "Su-li Ching-yun = %3.15f" %su_li
print "SGL             = %3.15f" %SGL
print "acos(-1.0)      = %3.15f" %acos(-1.0)
print "ACTUAL          = 3.14159265358979323846"