Turtle Geometry
Turtle Geometry
0001
Turtle Geometry
The Computer as a Medium for Exploring
Mathematics
Citation:
Turtle Geometry: The Computer as a Medium for Exploring
Mathematics
By: Harold Abelson, Andrea diSessa
DOI: 10.7551/mitpress/6933.001.0001
ISBN (electronic): 9780262362740
Publisher: The MIT Press
Published: 1986
Imagine that you have control of a little creature called a turtle that
exists in a mathematical plane or, better yet, on a computer display
screen. The turtle can respond to a few simple commands: FORWARD
moves the turtle, in the direction it is facing, some number of units.
RIGHT rotates it in place, clockwise, some number of degrees. BACK
and LEFT cause the opposite movements. The number that goes with a
command to specify how much to move is called the command's input.
A
(a) Turtle starts (b) FORWARD loo
/
(e) RIGHT 90 (d) FORWARD 150
LEFT 45
Figure 1.1
A sequence of turtle commands.
1.1.1 Procedures
Turtle geometry would be rather dull if it did not allow us to teach the
turtle new commands. But luckily all we have to do to teach the turtle a
new trick is to give it a list of commands it already knows. For example,
here's how to draw a square with sides loo units long:
TO SQUARE
FORWARD 100
RIGHT 90
FORWARD 100
RIGHT 90
FORWARD 100
RIGHT 90
FORWARD 100
This procedure will repeat the indented commands FORWARD 100 and
RIGHT 90 four times.
Another trick is to create a SQUARE procedure that takes an input for
the size of the square. To do this, specify a name for the input in the
title line of the procedure, and use the name in the procedure body:
TO SQUARE SIZE
REPEAT 4
FORWARD SIZE
RIGHT 90
Now, when you use the command, you must specify the value to be used
for the input, so you say SQUARE 100, just like FORWARD 100.
The chunk FORWARD SIZE, RIGHT 90 might be useful in other con-
texts, which is a good reason to make it a procedure in its own right:
TO SQUAREPIECE SIZE
FORWARD SIZE
RIGHT 90
Notice that the input to SQUARE, also called SIZE, is passed in turn as
an input to SQUAREPIECE. SQUAREPIECE can be used as a subprocedure
in other places as wellfor example, in drawing a rectangle:
TO RECTANGLE SIDE1 SIDE2
REPEAT 2
SQUAREPIECE SIDE1
SQUAREPIECE SIDE2
To use the RECTANGLE procedure you must specify its two inputs, for
example, RECTANGLE 100 50.
When programs become more complex this kind of input notation
can be a bit hard to read, especially when there are procedures such as
RECTANGLE that take more than one input. Sometimes it helps to use
parentheses and commas to separate inputs to procedures. For example,
the RECTANGLE procedure can be written as
TO RECTANGLE (SIDE1, SIDE2)
REPEAT 2
SQUAREPIECE (SIDE1)
SQUAREPIECE (SIDE2)
If you like, you can regard this notation as a computer language that
has been designed to make it easy to interact with turtles. Appendix
A gives some of the details of this language. It should not be difficult
to rewrite these procedures in any language that has access to the basic
turtle commands FORWARD, BACK, RIGHT, LEFT, PENUP, and PENDOWN.
TO TRY.ANGLE TO TRIANGLE
REPEAT 3 REPEAT 3
FORWARD loo FORWARD 100
RIGHT 60 RIGHT 120
Figure 1.2
Attempt to draw a triangle.
But TRY. ANGLE doesn't work, as shown in figure 1.2. In fact, running
this "triangle" procedure draws half of a regular hexagon. The bug in
the procedure is that, whereas we normally measure geometric figures
by their interior angles, turtle turning corresponds to the exterior angle
at the vertex. So if we want to draw a triangle we should have the
turtle turn 120°. You might practice "playing turtle" on a few geometric
figures until it becomes natural for you to think of measuring a vertex
by how much the turtle must turn in drawing the vertex, rather than by
>
TO HOUSE SIDE
SQUARE SIDE
TRIANGLE SIDE
TO HOUSE SIDE
SQUARE SIDE
FORWARD SIDE
RIGHT 30
TRIANGLE SIDE
Figure 1.3
(a) Initial attempt to draw a house fails. (b) Interface steps are needed.
the usual interior angle. Turtle angle has many advantages over interior
angle, as you will see.
Now that we have a triangle and a square, we can use them as building
blocks in more complex drawingsa house, for example. But figure 1.3
shows that simply running SQUARE followed by TRIANGLE doesn't quite
work. The reason is that after SQUARE, the turtle is at neither the correct
position nor the correct heading to begin drawing the roof. To fix this
bug, we must add steps to the procedure that will move and rotate
the turtle before the TRIANGLE procedure is run. In terms of designing
programs to draw things, these extra steps serve as an interface between
the part of the program that draws the walls of the house (the SQUARE
procedure) and the part that draws the roof (the TRIANGLE procedure).
In general, thinking of procedures as a number of main steps separated
by interfaces is a useful strategy for planning complex drawings.
Using procedures and subprocedures is also a good way to create
abstract designs. Figure 1.4 shows how to create elaborate patterns by
rotating a simple "doodle."
After all these straight line drawings, it is natural to ask whether the
turtle can also draw curvescircles, for example. One easy way to do
TO THING
FORWARD loo
RIGHT 90
FORWARD loo
RIGHT 90
FORWARD SO
RIGHT 90
FORWARD 50
RIGHT 90
FORWARD loo
RIGHT 90
FORWARD 25
RIGHT 90
FORWARD 25 TO THING1
RIGHT 90 REPEAT 4
FORWARD 50 THING
TO THING2 TO THING3
REPEAT FOREVER REPEAT FOREVER
THING THING
RIGHT 10 LEFT 45
FORWARD 50 FORWARD 100
Figure 1.4
Designs made by rotating a simple doodle.
Figure 1.5
FORWARD 1, RIGHT 1, repeated draws a circular arc.
this is to make the turtle go FORWARD a little bit and then turn RIGHT a
little bit, and repeat this over and over:
TO CIRCLE
REPEAT FOREVER
FORWARD i
RIGHT i
This draws a circular arc, as shown in figure 1.5. Since this program
goes on "forever" (until you press the stop button on your computer), it
is not very useful as a subprocedure in creating more complex figures.
More useful would be a version of the CIRCLE procedure that would
draw the figure once and then stop. When we study the mathematics of
turtle geometry, we'll see that the turtle circle closes precisely when the
turtle has turned through 3600. So if we generate the circle in chunks
of FORWARD i, RIGHT i, the circle will close after precisely 360 chunks:
TO CIRCLE
REPEAT 360
FORWARD i
RIGHT i
If we repeat the basic chunk fewer than 360 times, we get circular arcs.
For instance, 180 repetitions give a semicircle, and 60 repetitions give a
60° arc. The following procedures draw left and right arcs of DEG degrees
on a circle of size R:
TO ARCR R DEG
REPEAT DEG
FORWARD R
RIGHT i
TO ARCL R DEG
REPEAT DEG
FORWARD R
LEFT i
(See figure 1.6 and exercise 3 for more on making drawings with arcs.)
The circle program above actually draws regular 36Ogons, of course,
rather than "real" circles, but for the purpose of making drawings on
the display screen this difference is irrelevant. (See exercises 1 and 2.)
TO FLOWER SIZE
REPEAT 6
PETAL SIZE
RIGHT 60
TO RAY R
REPEAT 2
ARCL R 90
ARCR R 90
TO SUN SIZE
REPEAT 9
RAY SIZE
RIGHT 160 MONSTER
Figure 1.6
Some shapes that can be made using arcs.
on, let us begin by studying turtle geometry as a system in its own right.
Whereas studying coordinate geometry leads to graphs and algebraic
equations, turtle geometry will introduce sorne less familiar, but no less
important, mathematical ideas.
Intrinsic versus Extrinsic
One major difference between turtle geometry and coordinate geometry
rests on the notion of the intrinsic properties of geometric figures. An
intrinsic property is one which depends only on the figure in question,
not on the figure's relation to a frame of reference. The fact that a
rectangle has four equal angles is intrinsic to the rectangle. But the
fact that a particular rectangle has two vertical sides is extrinsic, for
an external reference frame is required to determine which direction is
"vertical." Turtles prefer intrinsic descriptions of figures. For example,
the turtle program to draw a rectangle can draw the rectangle in any
orientation (depending on the turtle's initial heading), but the program
CARTESIAN. RECTANGLE shown above would have to be modified if we
did not want the sides of the rectangle drawn parallel to the coordinate
axes, or one vertex at (O, O).
Another intrinsic property is illustrated by the turtle program for
drawing a circle: Go FORWARD a little bit, turn RIGHT a little bit, and
repeat this over and over. Contrast this with the Cartesian coordinate
representation for a circle, x2 + y2 = r2. The turtle representation
makes it evident that the curve is everywhere the same, since the process
that draws it does the same thing over and over. This property of the
circle, however, is not at all evident from the Cartesian representation.
Compare the modified program
TO CIRCLE
REPEAT FOREVER
FORWARD 2
RIGHT i
with the modified equation x2+2y2 = r2. (See figure 1.7.) The drawing
produced by the modified program is still everywhere the same, that is, a
circle. In fact, it doesn't matter what inputs we use to FORWARD or RIGHT
(as long as they are small). We still get a circle. The modified equation,
however, no longer describes a circle, but rather an ellipse whose sides
look different from its top and bottom. A turtle drawing an ellipse would
have to turn more per distance traveled to get around its "pointy" sides
REPEAT FOREVER
FORWARD 2
RIGHT i
C) x2 + 2j2 = r2
Figure 1.7
Modifying the turtle program still produces a circle. Modifying the equation gives
an ellipse.
than to get around its flatter top and bottom. This notion of "how
pointy something is," expressed as the ratio of angle turned to distance
traveled, is the intrinsic quantity that mathematicians call curvature.
(See exercises 2 and 4.)
Local versus Global
The turtle representation of a circle is not only more intrinsic than the
Cartesian coordinate description. It is also more local; that is, it deals
with geometry a little piece at a time. The turtle can forget about the
rest of the plane when drawing a circle and deal only with the small part
of the plane that surrounds its current position. By contrast, x2 + y2 =
r2 relies on a large-scale, global coordinate system to define its properties.
And defining a circle to be the set of points equidistant from some fixed
point is just as global as using x2 + y2 = r2. The turtle representation
does not need to make reference to that "faraway" special point, the
center. In later chapters we will see how the fact that the turtle does its
geometry by feeling a little locality of the world at a time allows turtle
geometry to extend easily out of the plane to curved surfaces.
Procedures versus Equations
A final important difference between turtle geometry and coordinate
geometry is that turtle geometry characteristically describes geometric
objects in terms of procedures rather than in terms of equations. In for-
mulating turtle-geometric descriptions we have access to an entire range
of procedural mechanisms (such as iteration) that are hard to capture in
ANGLE = i ANGLE = 60
v'vr
IA
rl
ANGLE = 135 ANGLE = 108
Figure 1.8
Shapes drawn by POLY.
Figure 1.9
Shapes drawn by NEWPOLY.
Recursion
One particularly important way to make new procedures and vary old
ones is to employ a program control structure called recursion; that is,
to have a procedure use itself as a subprocedure, as in
TO POLY SIDE ANGLE
FORWARD SIDE
RIGHT ANGLE
POLY SIDE ANGLE
The final line keeps the process going over and over by including "do
POLY again" as part of the definition of POLY.
ANGLE = 95 ANGLE = 90
Figure 1.10
Shapes drawn by POLYSPI.
Figure 1.10 shows some sample POLYSPI figures. Look carefully at how
the program generates these figures: Each time the turtle goes FORWARD
it goes one unit farther than the previous time.
Figure 1.11
The vertices of a POLYSPI.
A more general form of POLYSPI uses a third input (INC, for increment)
to allow us to vary how quickly the sides grow:
TO PQLYSPI (SIDE, ANGLE, INC)
FORWARD SIDE
RIGHT ANGLE
POLYSPI (SIDE + INC, ANGLE, INC)
ANGLE = O ANGLE = 40
INCREMENT = 7 INCREMENT = 30
ç_
,Aj
ANGLE = 2
INCREMENT = 20
Figure 1.12
Examples of INSPI.
Run INSPI and watch how it works. The turtle begins spiraling
inward as expected. But eventually the path begins to unwind as the
angle is incremented past 180°. Letting INSPI continue, we find that it
eventually produces a symmetrical closed figure which the turtle retraces
over and over as shown in figure 1.12. You should find this surprising.
Why should this succession of FORWARDs and RIGHTs bring the turtle back
precisely to its starting point, so that it will then retrace its own path?
We will see in the next section that this closing phenomenon reflects the
elegant mathematics underlying turtle geometry.
[P] Write a procedure that draws circular arcs. Inputs should specify
the number of degrees in the arc as well as the size of the circle. Can
you use the result of exercise 2 so that the size input is the radius of the
circle? [A]
[D] Which encloses the larger areaPOLY (5, 5) or POLY (6, 6)? [HA]
[P] Find inputs to INSPI that give a nonclosed figure. Can you give
a convincing argument that the figure is really nonclosed rather than,
say, a closed figure too big to fit on the display screen? [A]
[P] If the display system you are using allows "wraparound," you
can get some interesting effects by trying POLYs with very large sides.
Explore these figures. [H]
There are three kinds of "interchanges" we can perform on turtle
programs: interchanging RIGHT and LEFT, interchanging FORWARD and
BACK, and (for programs that terminate) reversing the sequence of in-
structions. Describe in geometric terms the effect of each of these opera-
tions, both by itself and in combination with the others. Start with the
class of programs that close (return the turtle to its initial position and
heading). [HA]
[P] The pattern made by the vertices of POLYSPI can be an interest-
ing object of study. The dots seem to group into various "arms," either
straight or curving left or right. To draw these patterns, you can use
the procedures
TO SPIDOT ANGLE
SUBSPIDOT O ANGLE
TO DOT
PENDOWN
FORWARD i
BACK i
PENUP
For example, predict what you will see between SPIDOT 90, which has
four arms, and SPIDOT 120, which has three. Can you explain the
sequence of figures you actually do see? Figure 1.11 shows how the figure
Use this program as the basis for some psychology experiments. For
instance, what is the average number of sides that must be drawn before
people can recognize which POLY it is?
[D] Find some local and intrinsic way to describe an ellipse. Write
a program that makes the turtle draw ellipses, where the inputs specify
the size and eccentricity of the ellipse. [A]
It's not hard to see why this formula is true. The number of sides
times the angle is precisely the total turning done by the turtle in
walking once around the figurethe net change in heading. If the
path is to close legitimately, and not just cross itself, then the turtle
must end its trip with the same heading it started out with. Thus,
the total turning must be some multiple of 3600.
Total turning is the central concept here. It certainly need not be
restricted to POLY. One can imagine any turtle program keeping a
running count of its turning, adding in RIGHTs and subtracting LEFTs.
Because only RIGHTs and LEFTs change heading, this total turning is
always exactly the total change in heading. In particular, if the path is a
closed path (one which restores the turtle's initial position and heading),
we can be confident that the net turning (= change of heading) is a
multiple of 3600. This gives us our first turtle-geometric theorem:
Figure 1.13
Rotation numbers of closed paths.
POLY Closing Theorem A path drawn by the POLY procedure will close
precisely when the total turning reaches a multiple of 360°.
There is one bug in this theorem, one exceptional case: If the angle of
the POLY is equal to O then the turtle just walks off along a straight
line. The path never closes, even though at every point the total
Figure 1.14
POLY lays down a sequence of equal chords.
Sketch of' Proof i Have you noticed the important fact that the vertices
of POLY lie on a circle? (Everything about POLY seems to be circular!)
We leave the proof of this geometric fact to you in exercise 2. Using
this fact, one can redescribe POLY as the sequential laying down of fixed-
length chords on a fixed circle as shown in figure 1.14. The point is that
there is only one chord of the required length that can be produced by
the turtle starting at any given heading. (Actually there are two, but
one of them has the wrong sensethe turtle would turn off the circle
after traversing the chord.) Thus, whenever the turtle returns to its
initial heading (total turning = any multiple of 360°) it will be about to
retrace the first chord and so should stop. Notice how this proof breaks
down for the exceptional case FORWARD SIDE, RIGHT 0. The turtle must
do some turning or else the vertices will lie on a straight line rather than
on a circle.
Pa
PO
(a) (b)
(e)
Figure 1.15
The POLY closing theorem. (a) Suppose the turtle returns to his initial heading, but
not his initial position. (b) n more steps must do the same thing again. (c) From a
new heading (after one POLY step), chunks of n steps carry the turtle away on a new
line.
thing again, moving the turtle farther out along the same line, and again
bring it back to the initial heading (figure 1.15b). Continuing with n
more repetitions, and n more, and so on, we see that the turtle must
run off infinitely far in the direction of the dotted line. Moreover, at no
point can the turtle's path stray very far from the line, since the turtle
must get back to it at the end of every n POLY steps.
Now let's return the turtle to the initial state and run the POLY step
for one iteration. We will now see the turtle at a new position pi with a
different heading. If we continue with n repetitions from here, the turtle
will end up on a new dotted line that lies at angle O to this new heading.
(figure 1.15c).
But the problem is obvious now. Running another sequence of n,
then another and another, forces the turtle off infinitely far along this
new line. But the turtle cannot remain close to both dotted lines as it
marches off to infinity. This contradiction means that our assumption
that the turtle does not come back to the initial position must have been
wrong. This completes the proof.
Note the use of the new symbol +, which means "assign to the variable
on the left the value given on the right." The procedure REMAINDER is
a function that computes the value of its first input modulo its second
input. The program also makes use of the iteration construct "REPEAT
UNTIL (some condition)", which keeps repeating the indented portion
until the condition is true (and always does the indented part at least
once).
Figure 1.16
Examples of POLYROLL.
Figure 1.17
Relate the angles A and B to the total turning over the arc.
FORWARD 10
LEFT 120
FORWARD O
LEFT 120
FORWARD 10
RIGHT 120
Can you give some motivation for pruning other than that it makes the
simple-closed-path theorem true? [A]
Fill in the details in the first proof of the POLY closing theorem (see
"sketch of proof 1"), including a proof that the vertices of POLY lie on a
circle. [H]
Prove that if the angle input to is an irrational number, the
POLY
turtle never returns to its initial position, and yet always remains within
a finite distance from it. [A]
[P] Invent some variations on the POLYROLL program, perhaps modeled
after POLYSPI and INSPI.
Rewrite the POLYSTOP program recursively, so that it doesn't use the
REPEAT command. [A]
What is the sum of the interior angles of an n-gon? What is the
interior angle of a regular n-gon? Show how these formulas can be easily
derived by using the simple-closed-path theorem. [HA]
Suppose we have a simple arc (an arc that does not cross itself) and
that we join the endpoints of the arc by a straight line. Suppose further
that the line and the arc do not intersect except at the endpoints (figure
1.17). Use the simple-closed-path theorem to give a formula relating the
Figure 1.18
Solve for A in terms of G.
total turning over the arc to the (interior) angles that the arc makes with
the line. [HA]
Apply the result of the previous exercise to find the angle between a
chord of a circle and the arc that it subtends (figure 1.18a). [Al
Use the previous exercise to compute the arc of a circle subtended by
an inscribed angle (figure l.18b). [HAI
Proof i of the POLY closing theorem was based on the fact that the
vertices of a POLY all lie on a circle. Use the simple-closed-path theorem
to show that the amount of arc on the circumscribed POLY circle from
one vertex to the next is just the angle input to the POLY procedure. IHI
We said that we would delay giving a proof of the simple-closed-path
theorem until chapter 4. Give a proof of the theorem in the special case
where the simple closed path is a POLY figure. [Hl
If you take a bicycle and lock the front wheel at angle O from straight
ahead (where O is rather small), the bicycle will turn in a circle. What
is the radius of the circle, given that the length between wheel centers
of the bicycle is D? [HA]
We said that the turtle approach allows us to take concepts that are
useful in thinking about computation and apply them to the study of
geometry. One such concept is that of state. Of course the idea of state
is not unique to computer science. It is important in physics, chemistry,
and any other field involving configurations that are subject to change.
But we do not generally look upon geometry in this way; geometric
figures are usually regarded as static objects. Turtle geometry provides
a more dynamic perspectivethe geometry is tied to movements.
The state of the turtle is given by specifying its position and its
heading. From the state point of view, the basic turtle commands-
FORWARD, BACK, LEFT, and RIGHTare state-change operators: They
cause the turtle to change state. In this section we will look at a sequence
of turtle commands purely in terms of its net effect in changing the initial
state to the final state, ignoring what comes between. Thus, a sequence
of turtle commands can be summarized as a single state-change operator.
At this level of abstraction all programs that generate a closed path are
the samethey are all state-change-equivalent. They correspond to the
simplest of all state-change operators, the one that does nothing and
leaves the initial state invariant.
90 72
135 180
60 144
Figure 1.19
NEWPOLYs and INSPIs with "POLY skeletons" indicated by dashed lines. (Numbers are
skeleton angles.)
Figure 1.20
(a) The net result (state change) of some basic ioop. (b) Repeating the net result
lays down the POLY skeleton.
You should be able to say what "has the structure of POLY" means in
detail. It includes such things as repeatedly touching base with a circle
RIGHT 2
RIGHT 2 + 20
RIGHT 2 + 2*20
RIGHT 2 + 3*20
RIGHT 2 + 17*20
RIGHT 2 + 18*20 = 2 + 360
The last command has the same effect as the first, and the one to follow,
RIGHT 2 + 19 )< 20 = RIGHT 2 + 20 + 360, is the same as the
second. The program is clearly staging a repeat performance of the first
18 steps. Computing the total turning, we find
2+(2+20)+(2+2 X 20)+...+(2+17 X 20)
= 18 X 2+(1 +2++11) X 20 = 3,096,
which is equal to 216, or 144, modulo 360. (When computing the
Figure 1.21
Spirolaterals.
If you study this procedure you will see that it amounts to having the
turtle draw an initial chunk of a POLYSPI (in fact, the first MAX lines of
a POLYSPI) and repeat this over and over. It is easy to see that the basic
loop in SPIRO has total turning ANGLE X MAX.
We will refer to the figures drawn by SPIRO as simple spirolaterals. A
more general kind of spirolateral (shown in figure 1.22) maintains a basic
loop in which each vertex has the same amount of turning, but allows the
turtle to turn left rather than right at some of the vertices. To specify
Figure 1.22
Generalized spirolaterals.
REPEAT
FORWARD SIDE * COUNT
IF MEMBER (COUNT, LIST)
THEN LEFT ANGLE
ELSE RIGHT ANGLE
COUNT - COUNT + i
UNTIL COUNT > MAX
Figure 123
Unexpectedly closed spirolaterals.
The basic loop SUBGSPIRO makes MAX - L right turns and L left turns
where L is the number of elements in LIST, making a total turning of
(MAX - L) X ANGLE - L X ANGLE = (MAX - 2L) X ANGLE.
The angle T is precisely the total turning of the fixed instruction se-
quence.
Looping Lemma and Classification of Looping Programs
Any program that repeats a fixed instruction sequence (or the equivalent,
as in INSPI) has the behavior of a POLY with angle T and side D in the
following senses (the angle T may be simply determined as the total
turning in the basic loop):
TO T=O
Figure 1.24
Any looping program is confined to a region (a) near a circle (T O) or (b) in the
exceptional case (T O) near a line.
[D] What is the heading change for INSPI with an ANGLE of A and
an INCREMENT of 360/n, where n is an integer? [HA]
[DF] Compute the total turning, T, of the basic loop in the following
looping program in terms of the angle inputs BOTTOM and TOP. Use
your formula to draw at least three different (ignoring size) figures with
threefold symmetry; fourfold, fivefold. [HA]
TO POLYARC (SIDE, BOTTOM, TOP)
REPEAT FOREVER
INSPI.STOP (SIDE, BOTTOM, TOP, 1)
INSPI.STOP (SIDE, TOP, BOTTOM, -1)
do loops
goto statements
assignment statements of the form x i- n
assignments of the form X - Y + n
conditional statements of the form IF X =n
For each kind of structure, say whether a program that repeats a block
of instructions consisting of basic turtle commands together with that
particular structure must necessarily be equivalent to a program which
repeats a fixed instruction sequence. Are there bad combinations, in the
sense that two structures which separately lead to looping may not loop
when combined in a single program?
Section 1.3 showed how the symmetry of any looping program is deter-
mined by the symmetry of an underlying POLY skeleton. But what deter-
mines the symmetry of the POLY? To begin with, it is clear that the SIDE
input in POLY SIDE ANGLE does not affect the shape of the figure at all
but only determines the size. The real question is: How does the ANGLE
input affect the symmetry of POLY? To be more precise, we can break
this question into two questions:
For a given ANGLE input, how many vertices will the resulting POLY have?
What we've done so far is little more than giving the answer a name.
How does one go about computing the least common multiple? One way,
which assumes that A is an integer, is to express A and 360 as products
of primes. Then each of the expressions nA and 360R will give a partial
view of the factorization of LCM(A, 360). For example, if A = 144 then
we have A = 2 X 32, 360 = 2 X 32 X 5. Using this decomposition, we
can deduce that
LCM(144, 360) = n X 2 X 32 = R X 2 X 32 x 5
2 X 32 X 5 = 720,
for it is easy to see that the LCM must contain at least four factors
of 2 (from A), one factor of 5 (from 360), and two factors of 3 (from
either A or 360), and from this we derive that n = 5 and R 2.
So POLY loo 144 has fivefold symmetry (it consists of n = 5 identical
pieces identically hooked together) and rotation number 2.
Another way to compute the least common multiple is to solve the
equation nA = 360R exactly as the procedure POLY solves it: by running
until it closes! Make a list of n X A for n = 1, 2, 3,... and see
Not only does the resulting pentagon not have tenfold symmetry, but it
has rotation number 1, not 2. We clearly are not justified in naming our
guesses 10 by n and 2 by R, so let's give them new names, n' and R'.
How do these relate to the real n and R?
Let's compute the real n and R that correspond to A = 360R'/n'. We
want the smallest positive integers n and R that satisfy nA = 360R. But
this equation is satisfied by all pairs of integers R and n with the property
that R/n = R'/n'. Since we want R and n to be the smallest pair with
this property, we should take them to be the numerator and denominator
of the fraction R'/n' reduced to lowest terms. In our example we had
R'/n' = 2/lo = 1/5, so n = 5 and R = 1. To put this another way,
A = 360R'/n' will give n'-fold symmetry only when R'/n' cannot be
reduced, that is to say, when R' and n' have no factors in common.
Thus, the answer to the second question above is the following:
Figure 1.25
Patterns of connecting eight points, n = 8.
tices modulo n, we see that the sequence of vertices hit are precisely the
multiples 0, R, 2R,.. . , (n - 1)R taken modulo n.
Now we saw in the previous subsection that, if R and n are relatively
prime, then the resulting POLY figure will have n vertices. In terms of the
circle picture this means that all vertices on the circle 0, 1, 2,.. . , n - i
are reached. Consequently, if R and n are relatively prime, then the
multiples of R taken modulo n must include all the numbers between O
and n - 1. In other words, if s is any integer between O and n - 1, then
there is some multiple of R, say pR, with pR = s (mod n). Moreover, if
R and n are not relatively prime, then at least one of the n vertices will
not be touched by the POLY process, and so there will be some number
s which is not equal to pR (mod n) for any p. Restating the equality
modulo n in terms of precise equality, we have the following:
Two integers R and n are relatively prime if and only if, for any integer
s, we can find integers p and q such that pR - qn = s.
Two integers R and n are relatively prime if and only if there exist
integers p and q such that pR + qs 1.
Euclid's Algorithm Start with two numbers. (1) If the two numbers are
equal, then stop; the GCD is their common value. (2) Subtract the
smaller number from the larger and throw away the larger. (3) Repeat
the entire process using, as the two numbers, the smaller number and
the difference computed in step 2.
The process produces ever smaller numbers and stops with two equal
numbers. Take as an example 360 and 144. The sequence of pairs
generated is
(360, 144) -+ (216, 144) - (72, 144) -+ (72, 72) - done: GCD = 72
Finding the GCD of 360 and 144 can be interpreted as follows: Any POLY
with an integer angle will touch some subset of the vertices of a regular
360-gon. If the angle is 144, the vertices touched will be the multiples
of 72.
We can translate Euclid's algorithm into a recursive computer proce-
dure:
TO EUCLID (N, R)
IF N = R THEN RETURN N
IF N > R THEN RETURN EUCLID (N - R, R)
IF N < R THEN RETURN EUCLID (N, R - N)
Note that Euclid's algorithm is very nearly the reverse of the "brute
force" method for finding the least common multiple given in subsection
1.4.1. In fact the algorithm gives us a new way of computing the LCM
of two numbers because of the formula
LCM(p, q) X GCD(p, q) = p X q,
which is true for any integers p and q. (See exercise 16.)
As a final remark we point out that, whereas the REMAINDER function
used in FAST . EUCLID is defined only for integers, the operations in the
original EUCLID procedure make sense for any numbers. So we can define
the GCD for any two numbers (not just integers) to be the value returned
by the EUCLID procedure. (Exercises 15-17 invite you to investigate the
properties of this generalized GCD.) We see here how the procedural
formulation of a concept can suggest new insights and directions for
exploring. Further explorations suggested by the EUCLID procedure are
illustrated in exercises 18-25.
(n) = n(1
i_)( 1)
10. Given integers R and n, show that there exist integers p and q such
that pR + qn i if and only if, for any integer s, there exist integers
p5 and q such that p5R - q5n = s. [A]
li. [D] Prove that if we define the greatest common divisor d =
GCD(n, R) as the largest integer that divides both n and R, then d is the
smallest positive integer that can be expressed as (integer)R+(integer)n.
Show also that the multiples of R taken modulo n are precisely the
multiples of d. [H]
[D] Show that the EUCLID procedure works when its inputs are
positive integers. That is, show that it will always terminate and that
what it returns is in fact the GCD of its inputs.
[P] In subsection 1.2.2 we discussed the problem of writing a POLY
procedure that draws the figure once and then stops. We implemented
POLYSTOP using the "local" strategy of having the turtle count total
turning. Write a version of POLYSTOP which uses the "global" strategy
of taking the ANGLE input and computing in advance how many times
to run the basic loop. Can you design the program so that it works not
only for integer angles but for (noninteger) rational angles as well?
Show directly that if Euclid's algorithm returns d given R and n as
inputs, then there exist integers p and q such that pR + qn = d. [H]
[D] Show that the EUCLID procedure terminates whenever its inputs
are positive rational numbers, and thus allows us to extend the definition
of greatest common divisor to all positive rational numbers. What is the
"GCD" of and ? of a/b and c/d where a, b, c and d are integers?
What can you say about the behavior of the algorithm when the inputs
are not both rational? [A]
[D] Prove for integers p and q that GCD(p, q) X LCM(p, q) = p X q.
Does this formula hold as well for rational numbers (with GCD defined
as the result of the EUCLID procedure and LCM defined as indicated in
exercise 4)?
[DI What can you say about the (integer)R + (integer)n definition
of GCD as it relates to the GCD for rational numbers (defined as the
result of the EUCLID procedure)?
[D] Modify the EUCLID procedure to define a procedure DIO which
not only computes cl = GCD(n, R) but also returns integers p and q
Why does this procedure run so slowly? Can you find a faster method
of computing the Fibonacci numbers? [A]
[DD[ Expanding on the result of exercise 21, investigate the greatest
common divisor of F(n) and F(n + k). State and prove a theorem about
GCD(F(a), F(b)). [HA]
[D} Use the DIO or FAST. DIO procedures (exercises 18,19) to solve
Diophantine equations of the form xF(n) + yF(n - 1) = i for integers
x and y. What is the solution in general? [HAI
[P] For any pair of numbers p, q we can define a new sequence of
numbers F(p, q; n) by applying the transformation S of exercise 21 begin-
ning with (p, q) rather than (1, 0). Write a program to generate these
sequences and investigate their properties. Can you express F(p, q; n) in
terms of the usual Fibonacci numbers? [A]
This book was set using the ThjX typesetting system by Michael Sannella
and printed and bound by Halliday Lithograph in the United States of
America.