Created
July 15, 2010 16:03
-
-
Save harikt/477157 to your computer and use it in GitHub Desktop.
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 | |
/* | |
* There may be always a better solution than this . | |
* Problem : Some discussion in ipgofkerala group regarding http://bit.ly/d2ihwR ( Eulers problem http://projecteuler.net/ ) . Many of them came with C#, Clojure, Ruby, Python.. more?? | |
* So I Just made some changes made by Sujeeth Nair in C++ to port to PHP | |
* I don't want PHP to miss this game :P | |
* Catch me at harikt.com | |
* Hari K T | |
*/ | |
$startTime = microtime(true); | |
$res = PrimeAt(10001); | |
$stopTime = microtime(true); | |
$diff = $stopTime - $startTime; | |
echo "Prime at 10001: $res and Milliseconds ellapsed $diff"; | |
function PrimeAt( $pos ) { | |
$p = 2; | |
$pos--; | |
while( $pos > 0 ) { | |
$p++; | |
$isPrime = true; | |
$n = 2; | |
for( ;$n*$n<$p+1;$n++) { | |
if( ( $p % $n ) == 0 ) { | |
$isPrime=false; | |
break; | |
} | |
} | |
if( $isPrime == true) { | |
$pos--; | |
} | |
} | |
return $p; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment