(Latest Revision: 09/22/2004)
Using Recursion to Compute Greatest Common Divisors
Discussion of Greatest Common Divisors:
Suppose that m and d are positive integers. We say d is a divisor of m (or
d divides m) if m = d * k for some positive integer k.
Example: d=5 is a divisor of m=50, because 50 = 5 * 10.
If m and n are positive integers then a positive integer d is a common
divisor of m and n if d divides m and d divides n.
Example: d=2 is a common divisor of m=78 and n=48, because 78 = 2 * 39 and 48
= 2 * 24.
If m and n are positive integers then m and n have a greatest common
divisor c. The number c is a positive integer which is a common divisor
of m and n. Also, c is a multiple of every common divisor of m and n. In
other words, c divides both m and n, and if k is some other integer that
divides both m and n, then k divides c too.
Example: 6 is the greatest common divisor of 78 and 48. 78 = 6 * 13 and 48 =
6 * 8. Other common divisors of 78 and 48, such as 2 and 3, are divisors of
6.
Notation: If m and n are positive integers, we denote their greatest common
divisor c as c = gcd(a,b).
Example: 6 = gcd(78,48)
Theorem: If m and n are positive integers and m <= n then either
- m divides n and m=gcd(m,n), or
- gcd(m,n) = gcd(n%m,m)
The Assignment:
Your assignment is to write a program that contains a function that uses the
idea of the theorem above to recursively compute greatest common divisors.
A sample run of such a program is included in this directory. You can view it
by clicking
here.
The function that computes greatest common divisors must input (as parameters)
any two positive int's m and n and return a positive int that is a greatest
common divisor of m and n. Here is a sample prototype for the function:
/* Recursive function that returns gcd(m,n) */
int gcd (int m, int n) ;
The function must be a recursive function. Also, the function must
print the messages you saw in the
sample output
having the form:
Working on <<first param>> and <<second param>>
...
To get those messages, you just put a cout statement at the very beginning of
the body of the function. The cout statement writes the values of the actual
parameters of the function. Because the function is recursive, the user sees
a report on the parameter values for each level of the recursion. This is
printed as an aid to understanding what is happening during the execution of
the recursive function.
I can give more details concerning the information above in class.
As you can see from examining the
sample run
of the program, your program is required to run interactively in a loop. It
prompts the user to enter two positive integers. If the user complies then
the program computes the gcd by executing the recursive function. The program
then reports the gcd of the two integers, which is the value returned by the
function. The program then prompts again. This continues while the user's
input is two positive integers. When the user enters two zero's, the program
terminates. Your program must not prompt for any other inputs, or attempt to
read any other inputs.
I plan to test your program by preparing some new sample inputs and placing
them in a file. My file will contain only pairs of integers. It will be a
file that looks similar to this:
36 28
91 21
856 20987
0 0
Of course the file above is only a sample. The actual test file(s) may have
more or fewer pairs of positive integers before the sentinel line of two
integers. (There will always be a sentinel line of two zero's at the end.)
I will test your program by running it in this manner:
a.out < myTestFile
where "myTestFile" is the name of the file I created that contains the list of
pairs of integers. Your program has to work correctly under those conditions.
To make sure, make some test files of your own and try your program on
them.
WHAT TO TURN IN:
This is not going to be a large program. It's probably going to be a level
two program with only about two or three small functions, including the main
function. Therefore, I am not requiring you to turn in a preliminary version.
You will turn in a printer output (hardcopy) and you will send me an e-mail
message. Please follow these rules:
- Always send me e-mail as plain text in the main message body.
Never send me attachments.
- Always use the exact subject line I specify for each
message. (I often get hundreds of e-mail messages in a week. The
subject line allows me to find, filter and sort messages.) You will lose
a significant number of points on the assignment if you use the wrong
subject line.
- Be very careful when you send the e-mail. You may use the
instructions in your
Hello World! lab excercise
for guidance. Of course, you will need to make the obvious changes to
those directions -- you have to use the correct subject line and
filename.
- Always send yourself a copy of each e-mail message you send to me,
and check immediately to see if you receive the message intact.
You are responsible for sending
e-mail correctly.
Here is the list of things you have to turn in:
- On the due date;
- At the start of class place a hardcopy of a
final level source file
of your program on the "counter" in front of me. Make sure all the
code is properly formatted and that it all shows on the paper.
- Using the subject line: CS2500,prog2.f send a
final level source file
of your program to me by e-mail before midnight.
- Using the subject line: CS2500,script2.f send a
final level test script
of your program to me by e-mail before midnight.
Note that there are no spaces in the subject lines given. It is important
that you do not insert any spaces. My e-mail address is: john@ishi.csustan.edu.
DUE DATE:
For the due date, see
the class schedule.