Published in: PHP
URL: http://reusablecode.blogspot.com/2008/05/euclids-algorithm.html
<?php /* PHP Mathematics Library - Euclid's Algorithm Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved. This work is licensed under the Creative Commons Attribution License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. */ // Determine the greatest common divisor of two numbers using Euclid's algorithm. function gcd($a, $b) { if ($a == 0) { return $b; } elseif ($b == 0) { return $a; } elseif ($a > $b) { return gcd($b, $a % $b); } else { return gcd($a, $b % $a); } } // Determine the least common multiple of two numbers using Euclid's algorithm. function lcm($a, $b) { if ($a > $b) { return ($b / gcd($a, $b)) * $a; } else { return ($a / gcd($a, $b)) * $b; } } ?>
You need to login to post a comment.
