Project Euler.net Answers
Problem 3
Solved on 2/29/2008
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? Note: This problem has been changed recently, please check that you are using the right number.
$number = 600851475143;
for($ic = 2; $ic <= sqrt($number); $ic++){
$thisint = $number / $ic;
$string = strval($thisint);
$isint = strpos($string, '.');
if($isint === FALSE){
$bn = $number / $ic;
if($this->isPrime($bn)){
$answer = $bn;
$ic = $number;
}else if(isPrime($ic)){
$answer = $ic;
}
}
}
function isPrime($val){
if(($val % 2 ) == 0) return FALSE;
for($c = 3; $c <= sqrt($val); $c+=2) {
if(($val % $c ) == 0) return FALSE;
}
return TRUE;
}
$answer = 6,857