here is an elegant recursive solution
<?php
function gcd($a,$b) {
return ($a % $b) ? gcd($b,$a % $b) : $b;
}
?>
gmp_gcdDescriptionresource gmp_gcd ( resource a, resource b )Calculate greatest common divisor of a and b. The result is always positive even if either of, or both, input operands are negative.
add a note
User Contributed Notes
bigkm1 at gmail dot com
26-Aug-2006 12:33
here is an elegant recursive solution
scr02001 at student dot mdh dot se
16-Aug-2003 09:58
If you do not consier a or b as possible negative numbers, a GCD funktion may return a negative GCD, wich is NOT a greatest common divisor, therefore a funktion like this may be better. This considers the simplyfying of (-3)-(-6) where gcd on -3 and -6 would result in 3, not -3 as with the other function. (-3)-(-6) is (-1)-(-2) NOT (1)-(2)
Ludwig Heymbeeck
13-Jan-2003 02:33
The following function is more accurate:
x-empt-php dot net at ispep dot cx
07-Jul-2002 02:16
No need to compile gmp functions in just for the GCD function... use this one instead: | |||