According to Donald Knuth in "The Art Of Computer Programming", the following is how to find the greatest common divisor of two positive numbers, m and n, using Euclid's Algorithm.


  
  Divide m by n and let r be the remainder.
  If r is 0, n is the answer; if r is not 0, continue to step 3.
  Set m = n and n = r. Go back to step 1.
  


Below is my implementation of this algorithm.

#include &lt;stdio.h&gt;

int greatestCommonDivisor(int m, int n)
{
    int r;

    /* Check For Proper Input */
    if((m == 0) || (n == 0))
        return 0;
    else if((m &lt; 0) || (n &lt; 0))
        return -1;

    do
    {
        r = m % n;
        if(r == 0)
            break;
        m = n;
        n = r;
    }
    while(true);

    return n;
}

int main(void)
{
    int num1 = 600, num2 = 120;
    int gcd = greatestCommonDivisor(num1, num2); 

    printf("The GCD of %d and %d is %d\n", num1, num2, gcd);

    getchar();
    return 0;
}


Though it appears to work just fine, it doesn't seem to be as elegant as I'd like, especially in the do-while loop.  Also, the while(true) seems dangerous, though I'm pretty confident that r will be reduced to 0 eventually and we'll break out of the loop.

Do you have any suggestions on either making the code more elegant or robust?


Question: Equivalent Key Signatures
Answer: The short (and oversimplified) answer is: Because Ab Major has fewer flats than G# Major has sharps, and thus it's easier for musicians to read.  This becomes especially apparent with keys such as D# Major, which has a double-sharp in it---the seventh note of the D# Major scale is not D, but Cx (that's "C double-sharp").

The longer and more accurate answer is that these keys are not in fact equivalent.  Only in an equal-tempered tuning system do G# and Ab have the same pitch, Eb and D# have the same pitch, etc.  That is, only in equal temperament do enharmonically equivalent notes actually have the same pitch.  In every other tuning system, the enharmonically equivalent notes are slightly (but definitely noticeably) different from each other.  If you've only been exposed to fixed-pitch instruments such as the piano and the guitar, this concept can be challenging at first, but violinists, wind, and brass players know that their intonation depends on the context of the key in which they're playing.

This in turn leads to different keys having different sonic characteristics.  In the modern, equal-tempered era, we've mostly lost these distinctions, but to composers of two hundred years ago and more, the keys of Ab and G# didn't sound the same at all, and they would compose to a specific key in order to take advantage of its particular character.

Update: In response to Brian's comment, I thought it would be helpful to post an example.  Here are links to YouTube videos, all three of which were posted by the same person, using the same synthesizer, all three playing Bach's Air on the G String.  But each link uses a different tuning system:


Equal Temperament: http://www.youtube.com/watch?v=c6XkgNT20Eg
Just Intonation: http://www.youtube.com/watch?v=gdL8aPQUOk0
Pythagorean Tuning: http://www.youtube.com/watch?v=hq-SrgV7_Ow