3
This commit is contained in:
parent
6200a1a235
commit
4008086476
39
euler3.php
Normal file
39
euler3.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
/*
|
||||
* Gnieark's anwser to euler problem 3
|
||||
* https://projecteuler.net/problem=3
|
||||
*/
|
||||
|
||||
echo largestPrimeFactorOf(600851475143);
|
||||
function largestPrimeFactorOf($number){
|
||||
$primesNumbers=array(2);//non considering value 1
|
||||
$i=3; //begin to 3
|
||||
|
||||
while($i<sqrt($number)){
|
||||
//echo $i;
|
||||
//is $i a factorOf $number
|
||||
if(fmod($number,$i)==0){
|
||||
//is $i a prime number
|
||||
$index=0;
|
||||
$isPr=true;
|
||||
while(($primesNumbers[$index]<sqrt($i)) && $index < (count($primesNumbers))-1){
|
||||
if((fmod($i,$primesNumbers[$index])==0)){
|
||||
$isPr=false;
|
||||
break;
|
||||
}
|
||||
$index++;
|
||||
}
|
||||
if($isPr){
|
||||
$primesNumbers[]=$i;
|
||||
$lastPr=$i;
|
||||
}
|
||||
|
||||
}
|
||||
$i=$i+2;
|
||||
}
|
||||
if (isset($lastPr)){
|
||||
return $lastPr;
|
||||
}else{
|
||||
return 1;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user