Greatest Common Divisor, Recursively

Note: The next three paragraphs are taken largely from Wikipedia's Euclidean algorithm entry. End of note.

The Euclidean algorithm (described by the Greek mathematician Euclid in his mathematical treatise Elements in 300 BC) is based on the principle that the greatest common divisor (GCD) of two numbers does not change if the larger number is replaced by its difference with the smaller number. For example, 21 is the GCD of 252 and 105, and the same number 21 is also the GCD of 105 and 252 − 105 = 147. Since this replacement reduces the larger of the two numbers, repeating this process gives successively smaller pairs of numbers until the smaller of the two numbers becomes zero. When that occurs, the larger one is the GCD of that pair and hence the GCD of the original pair of numbers.

The version of the Euclidean algorithm described above (and by Euclid) can take many subtraction steps to find the GCD when the larger of the given numbers is much bigger than the other. A more efficient version of the algorithm shortcuts these steps, instead replacing the larger of the two numbers by its remainder when divided by the smaller of the two. (With this version, the algorithm stops when that remainder is zero.)

With this improvement, the algorithm never requires more steps than five times the number of digits (base 10) of the smaller integer. This was proven by Gabriel Lame in 1844, and marks the beginning of computational complexity theory (which is concerned, in part, with how much "work" an algorithm performs as a function of how much input data it is given).

In summary, assuming that k≥m≥0 and k+m > 0:

If m is zero, then GCD(k,m) = k; otherwise, GCD(k,m) = GCD(m, k%m).

Using this information, supply the body of the gcdViaRec() method in the GCD_Tester Java application that is provided. Use the application to test your method for correctness. Entering a negative value for k causes the program to terminate.