Last active
August 29, 2015 14:07
-
-
Save zigo928/c97e97fb89bbfe608af8 to your computer and use it in GitHub Desktop.
最大公约数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function maxDivisor($a, $b) | |
{ | |
if (! $a || !$b) { | |
return false; | |
} | |
if ($b > $a) { | |
list($a, $b) = array($b, $a); | |
} | |
while(true) { | |
$div = $a % $b; | |
if ($div === 0) { | |
return $b; | |
} | |
$a = $b | |
$b = $div; | |
maxDivisor($a, $b); | |
} | |
return 1; | |
} | |
// method 2 | |
function maxDiv($a, $b) | |
{ | |
if (! $a || !$b) { | |
return false; | |
} | |
if ($b > $a) { | |
list($a, $b) = array($b, $a); | |
} | |
for ($i = $b; $i > 0; $i--) { | |
if (is_int($a/$i) && is_int($b/$i)) { | |
return $i | |
} | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment