note 66252 added to function.gmp-gcd
| From: | php-general at lists dot php dot net | Date: | Wed, 17 May 2006 20:47:13 +0000 |
| Subject: | note 66252 added to function.gmp-gcd | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-111524@lists.php.net to get a copy of this message | ||
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 */
}
?>
----
Server IP: 216.194.113.175
Probable Submitter: 68.238.245.163
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.gmp-gcd.php
Edit -- http://master.php.net/note/edit/66252
Del: integrated -- http://master.php.net/note/delete/66252/integrated
Del: useless -- http://master.php.net/note/delete/66252/useless
Del: bad code -- http://master.php.net/note/delete/66252/bad+code
Del: spam -- http://master.php.net/note/delete/66252/spam
Del: non-english -- http://master.php.net/note/delete/66252/non-english
Del: in docs -- http://master.php.net/note/delete/66252/in+docs
Del: other reasons-- http://master.php.net/note/delete/66252
Reject -- http://master.php.net/note/reject/66252
Search -- http://master.php.net/manage/user-notes.php