✐

✐

“91974˙CH02˙ﬁnal” — 2012/12/14 — 13:58 — page 80 — #36

✐

✐

✐

✐

✐

✐

80 CHAPTER 2

π

thon

2.7 Summary

This chapter has introduced the following additional building blocks of programming and

Python:

Math modules

Boolean expressions

Selection statements

One very important pattern, the accumulator pattern, was also introduced. This pattern

uses an accumulator variable that is continuously updated as part of an iteration. It is

important to initialize the accumulator variable to an appropriate starting value. Finally,

we used the Monte Carlo simulation technique, which is based on random numbers, to

approximate the value of pi.

Key Terms

accumulator pattern initialization relational expression

accumulator variable Leibniz formula relational operator

approximation logical operator running sum

Archimedes approach Monte Carlo simulation selection statement

Boolean expression nested selection short-circuit evaluation

compound Boolean expression pi tail nesting

degrees radians Wallis formula

Python Keywords

and help math random

elif if else not return

False if or sin

for import print True

✐

✐

“91974˙CH02˙ﬁnal” — 2012/12/14 — 13:58 — page 81 — #37

✐

✐

✐

✐

✐

✐

Programming Exercises 81

Bibliography

[AAR04] Runde Angel, Christine Abbot, and Dennis Runde. A Survey of Mathematics

with Applications. Prentice Hall, 2004.

[BB87] J. M. Borwein and P. B. Borwein. Ramanujan and pi. Scientiﬁc American, 1987.

[Wei02] Eric W. Weisstein. CRC Concise Encyclopedia of Mathematics. Chapman & Hall/

CRC, 2002.

Programming Exercises

2.1 Write a function to compute the circumference of a circle with a radius of r.User

as the parameter to the function. Use π from the math module.

2.2 Write a function to compute the area of a circle with a radius of r.User as the

parameter to the function. Use π from the math module.

2.3 Write a function to compute the volume of a sphere with a radius of r.User as the

parameter to the function. Use π from the math module.

2.4 Write a function that will return an approximate value for π by evaluating the equa-

tion π = 16(arctan

1

5

) − 4(arctan

1

239

), where arctan is the arc tangent function from

the math library.

2.5 Write a function that will return an approximate value for π by evaluating the equa-

tion π =

ln(640320

3

+744)

√

163

, where ln is the log function from the math library.

2.6 Write a function isEven that takes a number as a parameter and returns True if the

number is even and False otherwise.

2.7 Write a function that will return an approximate value for π by evaluating the equa-

tion π =

√

12(1 −

1

3×3

+

1

5×3

2

−

1

7×3

3

+ .... Use a parameter called n to denote the

number of terms to include in the summation.

2.8 We have used the square root function from the math library a number of times in

this chapter. Write your own square root approximation function using the equation

X

k+1

=

1

2

× (X

k

+

n

X

k

) where X

0

= 1. This equation says that the

√

n can be found

by repeatedly computing the next X

i

term. The larger the number of terms used, the

better the answer. Allow your function to have two input parameters, the number

that you want the square root of and the number of terms to compute.

✐

✐

“91974˙CH02˙ﬁnal” — 2012/12/14 — 13:58 — page 82 — #38

✐

✐

✐

✐

✐

✐

82 CHAPTER 2

π

thon

2.9

Buﬀon’s needle is a very old (eighteenth-century) simulation for approximating pi.

Research this simulation and implement it using Python and turtle graphics. How

good is the approximation?

Start Free Trial

No credit card required