Skip to content

Instantly share code, notes, and snippets.

@harikt
Created July 15, 2010 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harikt/477157 to your computer and use it in GitHub Desktop.
Save harikt/477157 to your computer and use it in GitHub Desktop.
<?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