Skip to content

Instantly share code, notes, and snippets.

@Francis-Njoku
Created January 6, 2020 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Francis-Njoku/673c518161faad5f3e99b151a83151c9 to your computer and use it in GitHub Desktop.
Save Francis-Njoku/673c518161faad5f3e99b151a83151c9 to your computer and use it in GitHub Desktop.
Find the largest prime factor of the number 600851475143
<?php
function find_highest_prime_factor($n)
{
for ($i = 2; $i <= $n; $i++)
{
if (bcmod($n, $i) == 0) //its a factor
{
return max($i, $this->find_highest_prime_factor(bcdiv($n,$i)));
}
}
if ($i == $n)
{
return $n; //it's prime if it made it through that loop
}
}
$n = 600851475143;
echo find_highest_prime_factor($n);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment