Dijkstra's Algorithm for finding the GCD of a number states that, if m > n,
then GCD(m,n) equals GCD(m-n,n). The reason for this is, if m/d and n/d both have
no remainder, then (m-n)/d leaves no remainder (this is very clever. Dijkstra
was a very smart man). This leads to the algorithm that
For m,n > 0, gcd(m,n) =
m if m=n
gcd(m - n,n) if m > n
gcd(m,n-m) if m < n
So, for example, finding the GCD of 21 and 9
gcd(21,9), so m=21 and n=9. m is greater than n, so we do
gcd(m-n,n) which is ( 21 - 9, 9 ) or gcd ( 12, 9 ) m still greater than n, so
gcd(m-n,n) or ( 12 - 9, 9 ) = gcd ( 3, 9 ), n is now greater than m, so
gcd(m,n-m) or ( 3, 9 - 3 ) = gcd ( 3, 6 ), n is still greater than m, so
gcd(m,n-m) or ( 3, 6 -3 ) = gcd ( 3, 3 ). They are now equal, so gcd = 3
Write a recursive function that implements this algorithm, and a small program t
o test it.