note 69806 deleted from ref.bc by nlopess
| From: | nlopess@php.net | Date: | Fri, 22 Sep 2006 17:36:15 +0000 |
| Subject: | note 69806 deleted from ref.bc by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-117447@lists.php.net to get a copy of this message | ||
Note Submitter: suggestions at freephpcalculator dot tk
----
The Greatest Common Denominator of two large numbers, using BCMath functions. Please read the PHP
manual for BCMath requirements.
It works even for those who cannot have GMP support in their PHP distribution, for instance due to
the web host policy.
You can see a demo here:
http://www.freephpcalculator.tk/
Code:
// License: free to do whatever you want with the code
// even using it
function bcgcd ($value1, $value2) {
if ($value1 < $value2)
// Swap $value1 and $value2
{
$temp = $value1;
$value1 = $value2;
$value2 = $temp;
}
// We use the Euclid's algorithm
// for finding the Greatest Common Denominator (GCD)
$mod = 1;
while ($mod != 0)
{
$mod = bcmod ($value1, $value2);
$value1 = $value2;
$value2 = $mod;
}
return $value1;
}