Project Euler Problem 3
Project Euler Problem 3: Find the largest prime factor of a composite number.
<?php
function lpf($n)
{
if ($n < 2) return false;
for ($p = 2; $p <= 3; $p++)
while (!($n%$p))
if (($n /= $p) == 1) return $p;
for ($p = 5, $d = 1; pow($p, 2) <= $n;
$p += 3-$d, $d *= -1)
while (!($n%$p))
if (($n /= $p) == 1) return $p;
return $n;
}
echo lpf(600851475143);
?>











0 Comments...
Post a Comment