Created
January 6, 2020 04:46
-
-
Save Francis-Njoku/673c518161faad5f3e99b151a83151c9 to your computer and use it in GitHub Desktop.
Find the largest prime factor of the number 600851475143
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 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