Skip to content

Instantly share code, notes, and snippets.

@E1101
Created December 17, 2017 08:55
Show Gist options
  • Save E1101/fa706f6664277f3d160e6f2f9b4f5401 to your computer and use it in GitHub Desktop.
Save E1101/fa706f6664277f3d160e6f2f9b4f5401 to your computer and use it in GitHub Desktop.
Get A Number In Prime Series By Index; The nTH Prime.
<?php
function getPrimingSeriesIndex($index)
{
if ( $index <= 0 )
return false;
if ($index == 1)
return 2;
$offset = 1;
do {
(! isset($primeNum)) ? $primeNum = 3 : $primeNum++;
$flagNumDivided = 0;
for ( $i = 1; $i <= $primeNum; $i++) {
if ( ($primeNum % $i) == 0 ) {
if (++$flagNumDivided > 2)
// sure; its not prime
break;
}
}
if ($flagNumDivided <= 2)
$offset++;
if ($offset == $index)
return $primeNum;
} while(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment