O'Reilly logo

Python Programming in Context, 2nd Edition by David L. Ranum, Bradley N. Miller

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

“91974˙CH02˙final” 2012/12/14 13:58 page 49 #5
2.4 The Archimedes Approach 49
In the sections that follow, we will explore a number of interesting techniques for approxi-
mating pi. Remember that none of them can give us an exact answer.
Exercises
2.1 Run the help command as follows: help('turtle'). Note the quotes around turtle.
2.2 Run the help command as follows: help(turtle). What is the difference?
2.3 import turtle and repeat the last exercise.
2.4 Run the help command on the math.sin function.
2.5 Explore the random module using the help facility. What does the randrange function
do? What does the randint function do?
2.4 The Archimedes Approach
The first technique we will consider, attributed to the mathematician Archimedes, makes
use of a many-sided polygon to approximate the circumference of a circle. Recall that pi
is related to the circumference of a circle by the equation C =2πr where r is the radius of
the circle. Given a circle of radius 1, sometimes called a unit circle, this equation can be
rearranged to give π =
C
2r
or simply π =
C
2
.
The Archimedes approach, shown in Figure 2.2, will use the distance around a polygon
that is inscribed within a unit circle. By using a polygon with increasing numbers of sides
(therefore decreasing side length), the total distance around the polygon will come closer
and closer to the actual circumference of the circle. Does this sound familiar? Of course,
no matter how large the number of sides, we will never truly reach the actual circle.
Figure 2.2 shows more of the details that will be needed to understand how this approxi-
mation will work. If we assume that the polygon has n sides of length s, we can then focus
our attention on a small slice of the polygon. In the triangle shown, the side labeled h will
have a length of 1 since we are assuming a unit circle. The angle labeled B can be easily
computed by remembering that there are 360 degrees in a circle. This means that angle
B is 360 ÷ n. Therefore, angle A is
1
2
B. In addition, we also know that the highlighted
triangle is a right triangle so that the side opposite angle A has a length of
1
2
s
Now we have to use a bit of trigonometry. In a right triangle (see Figure 2.3), the ratio of
the opposite side to the long side (or hypotenuse) is equal to the sine of angle A. Because
our triangle has a hypotenuse of length 1, we know that
1
2
s will simply be equal to the
“91974˙CH02˙final” 2012/12/14 13:58 page 50 #6
50 CHAPTER 2
π
thon
s
B
A
h
1/2 s
Figure 2.2 Developing the Archimedes Approach using an 8-sided polygon
A
Hypotenuse
Opposite
Opposite
Hypotenuse
sin A =
Figure 2.3 The sin function
sine of angle A. How do we compute the sine of angle A? The answer is to use the math
library. As with sqrt earlier, the sin function (as well as all of the other trigonometric
relationships) is available once we import the math module.
2.4.1 The Python Implementation
Session 2.3 shows that it is possible to implement Archimedes’ algorithm by using the
interactive Python environment. We can simply type in and evaluate statements that follow
the steps outlined here. Note that the last step evaluates the value of pi as computed in
the previous statement.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required