GCD function in c++ sans cmath library

前端 未结 5 994
悲哀的现实
悲哀的现实 2020-12-14 01:54

I\'m writing a mixed numeral class and need a quick and easy \'greatest common divisor\' function. Can anyone give me the code or a link to the code?

5条回答
  •  失恋的感觉
    2020-12-14 02:44

    The Euclidean algorithm is quite easy to write in C.

    int gcd(int a, int b) {
      while (b != 0)  {
        int t = b;
        b = a % b;
        a = t;
      }
      return a;
    }
    

提交回复
热议问题