(Latest Revision: 03/10/2000)
03/10/2000 -- added a few simple examples.

Using Recursion to Compute Greatest Common Divsors


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

  1. m divides n and m=gcd(m,n), or
  2. 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 three functions, including the main function. Therefore, I am not requiring you to turn in a preliminary version.

Turn in a final-level program. Before midnight on the due date, send via e-mail Important! Use the following subject lines for your e-mailings:

for the source file: "CS2500P2Source"
for the script file: "CS2500P2Script"

It will really help me a lot in sorting my mail if you use exactly those subject lines. (Note there are no spaces.)


DUE DATE:

For the due date, see the class schedule.