note 66252 deleted from function.gmp-gcd by nlopess

From: Date: Wed, 17 May 2006 21:48:29 +0000
Subject: note 66252 deleted from function.gmp-gcd by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-111527@lists.php.net to get a copy of this message
Note Submitter: ---- You may want to use the binary GCD function since its faster. <?php function gcd($u, $v) { $u = abs($u); $v = abs($v); if ($u == 0) return $v; if ($v == 0) return $u; $k = 0; while (($u & 1) == 0 && ($v & 1) == 0) { /* while both u and v are even */ $u >>= 1; /* shift u right, dividing it by 2 */ $v >>= 1; /* shift v right, dividing it by 2 */ $k++; /* add a power of 2 to the final result */ } /* At this point either u or v (or both) is odd */ do { if (($u & 1) == 0) /* if u is even */ $u >>= 1; /* divide u by 2 */ else if (($v & 1) == 0) /* else if v is even */ $v >>= 1; /* divide v by 2 */ else if ($u >= $v) /* u and v are both odd */ $u = ($u-$v) >> 1; else /* u and v both odd, v > u */ $v = ($v-$u) >> 1; } while ($u > 0); return $v << $k; /* returns v * 2^k */ } ?>

« previous php.notes (#111527) next »