分类:
2006-07-13 10:32:10
function gcd1($m,$n) { return ($m % $n == 0) ? $n : gcd1($n,$m % $n); } function gcd2($m,$n){ return ($m < $n) ? gcd2($m,$n % $m) : gcd2($n, $m % $n); } function gcd3($m, $n){ while($n > 0){ $r = $m % $n; $m = $n; $n = $r; } return $m; } function gcd4($m, $n){ if($m == $n) return $n; elseif($m < $n) return gcd4($m, $n-$m); else return gcd4($m-$n,$n); } function gcd5($m, $n){ while($m != $n){ if($m < $n) $n -= $m; else $m -= $n; } return $n; } |
呵,有递归有迭代,不过没有考虑非法参数的处理,如负数或0等,朋友们如果有其它的方法也请赐教..