verry faster
This commit is contained in:
parent
6f995bb87d
commit
513f500b22
42
_functions.php
Normal file
42
_functions.php
Normal file
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
function isPrimary($number){
|
||||
$i=1;
|
||||
while(getPrimeNumber($i) <= sqrt($number)){
|
||||
if(fmod($i,getPrimeNumber($i))==0){
|
||||
return false;
|
||||
}
|
||||
$i=$i+2;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function getPrimeNumber($order){
|
||||
|
||||
static $primeNumbersList=array(2,3);
|
||||
//if already finded:
|
||||
if(isset($primeNumbersList[$order-1])){
|
||||
return $primeNumbersList[$order-1];
|
||||
}
|
||||
|
||||
$i=$primeNumbersList[count($primeNumbersList)-1] + 2;
|
||||
|
||||
while (count($primeNumbersList) < $order){
|
||||
$isPrimaryNumber=true;
|
||||
foreach($primeNumbersList as $pr){
|
||||
|
||||
if($pr>sqrt($i)){
|
||||
break;
|
||||
}
|
||||
if(fmod($i,$pr)==0){
|
||||
$isPrimaryNumber=false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if($isPrimaryNumber){
|
||||
$primeNumbersList[]=$i;
|
||||
}
|
||||
$i=$i+2;//sert à rien de tester les nombres pairs
|
||||
}
|
||||
return $i-2;
|
||||
}
|
38
euler03.php
38
euler03.php
|
@ -3,37 +3,19 @@
|
|||
* Gnieark's anwser to euler problem 3
|
||||
* https://projecteuler.net/problem=3
|
||||
*/
|
||||
require_once("_functions.php");
|
||||
|
||||
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;
|
||||
$right=$number;
|
||||
$i=1;
|
||||
$prFact=1;
|
||||
while(getPrimeNumber($i)<=$right){
|
||||
if(fmod($right,getPrimeNumber($i))==0){
|
||||
$prFact=getPrimeNumber($i);
|
||||
$right=$right/$prFact;
|
||||
}
|
||||
$index++;
|
||||
}
|
||||
if($isPr){
|
||||
$primesNumbers[]=$i;
|
||||
$lastPr=$i;
|
||||
}
|
||||
|
||||
}
|
||||
$i=$i+2;
|
||||
}
|
||||
if (isset($lastPr)){
|
||||
return $lastPr;
|
||||
}else{
|
||||
return 1;
|
||||
$i++;
|
||||
}
|
||||
return $prFact;
|
||||
}
|
Loading…
Reference in New Issue
Block a user