-4

I just started reading volume 1 of Knuth's art of programming and got to the section on page 4 where he describes the Euclidean algorithm. He states the steps to find the greatest common divisor of two numbers. You can read more about it here https://en.wikipedia.org/wiki/Euclidean_algorithm

He steps through an example of finding the GCD of 199 and 544 which he states 17 as the answer. I quickly implemented the algorithm just to follow along but was getting the answer 1. Using my calculator I also get the answer 1.

my code is.

#import <math.h>
#import <stdio.h>

double GreatestComminDivisor(int m, int n) {

    double r = fmod(m, n);
    if (m < n) {
        int temp = n;
        n = m;
        m = temp;
    }

    while (r != 0) {
        r = fmod(m, n);

        if (r == 0) {
            return n;
        }

        m = n;
        n = r;
    }

    return n;
}

int main(int argc, char const *argv[]) {

  int m = 199;
  int n = 544;
  double answer = GreatestComminDivisor(m, n);

  printf("GCD of %i and %i is %f \n", m, n, answer);

  return 0;
}

Am I missing something in his example of did he get the answer wrong. I have tried to look up the errata for the third edition of the book and not getting any results. Just want to make sure I'm not going mad.

Lee Dale
  • 1,136
  • 9
  • 20
  • 7
    17 is not a divisor in 199 nor in 898 which is the number you have in your source code. Perhaps you have misread the number from Knuths book? I highly doubt that Knuth would make a simple mistake like that. – Martin Liversage Aug 18 '15 at 06:35
  • 3
    may be it is a printing mistake. If you only knew what GCD is, you would not have asked this – vish4071 Aug 18 '15 at 06:38
  • Sorry the 898 was a mistake when I copied into the question. I have changed it to the numbers Knuth stated in his book. He definitely states the GCD of 199 and 544 is 17. My question is am I missing something or is it a mistake in the book? – Lee Dale Aug 18 '15 at 06:52
  • @vish4071 Sorry why do you hate this question? Chill out mate, if you don't like the question then don't spend time on it. What's with the hostility? – Lee Dale Aug 18 '15 at 06:53
  • @LeeDale: 199 is not divisible by 17. It's probably a typo for 119, which is. – dan04 Aug 18 '15 at 06:53
  • @LeeDale You get down votes because you typed in the wrong numbers. Fix them and people may remove their down votes. It very much seems that Knuth used 544 and 119, see [this](http://math.stackexchange.com/questions/815483/whats-the-meaning-of-544-119-4-68-119) for example. – Lundin Aug 18 '15 at 06:59
  • I just checked my copy of *Knuth* (3rd edition): the numbers used are `119` and `544`. – pmg Aug 18 '15 at 18:47

2 Answers2

2

Here's an example of a GCD function

int gcd( int a, int b )
{
    if ( b == 0 )
        return( a );
    else
        return( gcd( b, a % b ) );
}

And the numbers are 119 and 544.

user3386109
  • 34,287
  • 7
  • 49
  • 68
  • Thanks my question is more about the answer in the book and whether I am missing something with the algorithm that would make my answer of 1 wrong. – Lee Dale Aug 18 '15 at 06:55
  • The gcd(199,544) is 1. The gcd(119,544) is 17. I'll let you draw your own conclusions from that. – user3386109 Aug 18 '15 at 06:58
1

Recursion-free algorithm based on the already accepted answer:

int gcd (int a, int b)
{
  int mod;

  while((mod = a % b) != 0)
  {
    a = b;
    b = mod;
  }

  return b;
}
Lundin
  • 195,001
  • 40
  • 254
  • 396