]> git.ozlabs.org Git - ccan/blob - junkcode/tterribe@email.unc.edu-nmbrthry/gcd.c
tlist: typesafe variant of list module.
[ccan] / junkcode / tterribe@email.unc.edu-nmbrthry / gcd.c
1 #include "gcd.h"
2
3 /*Computes the gcd of two integers, _a and _b.
4   _a: The first integer of which to compute the gcd.
5   _b: The second integer of which to compute the gcd.
6   Return: The non-negative gcd of _a and _b.
7           If _a and _b are both 0, then 0 is returned, though in reality the
8            gcd is undefined, as any integer, no matter how large, will divide 0
9            evenly.*/
10 int gcd(int _a,int _b){
11   /*Make both arguments non-negative.
12     This forces the return value to be non-negative.*/
13   if(_a<0)_a=-_a;
14   if(_b<0)_b=-_b;
15   /*Simply use the Euclidean algorithm.*/
16   while(_b){
17     int r;
18     r=_a%_b;
19     _a=_b;
20     _b=r;
21   }
22   return _a;
23 }