X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=junkcode%2Ftterribe%40email.unc.edu-nmbrthry%2Fgcd.c;fp=junkcode%2Ftterribe%40email.unc.edu-nmbrthry%2Fgcd.c;h=bfd54c6bf444f4907a6ae99dd4356c834a40efdf;hp=0000000000000000000000000000000000000000;hb=a1deddc3b3f04f670185a72816aca363a7aba9c2;hpb=6f17eb5b82edeced6910fb353714e4012a74960d diff --git a/junkcode/tterribe@email.unc.edu-nmbrthry/gcd.c b/junkcode/tterribe@email.unc.edu-nmbrthry/gcd.c new file mode 100644 index 00000000..bfd54c6b --- /dev/null +++ b/junkcode/tterribe@email.unc.edu-nmbrthry/gcd.c @@ -0,0 +1,23 @@ +#include "gcd.h" + +/*Computes the gcd of two integers, _a and _b. + _a: The first integer of which to compute the gcd. + _b: The second integer of which to compute the gcd. + Return: The non-negative gcd of _a and _b. + If _a and _b are both 0, then 0 is returned, though in reality the + gcd is undefined, as any integer, no matter how large, will divide 0 + evenly.*/ +int gcd(int _a,int _b){ + /*Make both arguments non-negative. + This forces the return value to be non-negative.*/ + if(_a<0)_a=-_a; + if(_b<0)_b=-_b; + /*Simply use the Euclidean algorithm.*/ + while(_b){ + int r; + r=_a%_b; + _a=_b; + _b=r; + } + return _a; +}