note 35029 added to function.gmp-gcd
| From: | scr02001 at student dot mdh dot se | Date: | Sat, 16 Aug 2003 11:58:39 +0000 |
| Subject: | note 35029 added to function.gmp-gcd | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-54338@lists.php.net to get a copy of this message | ||
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)
function eGCD($a,$b){
if($a < 0) $a=0-$a;
if($b < 0 ) $b=0-$b;
if($a == 0 || $b == 0) return 1;
if($a == $b) return a;
do{
$rest=(int) $a % $b; $a=$b; $b=$rest;
}while($rest >0);
return $a;
}
----
Manual Page -- http://www.php.net/manual/en/function.gmp-gcd.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35029
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35029&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35029&report=yes