Created
December 17, 2017 08:55
-
-
Save E1101/fa706f6664277f3d160e6f2f9b4f5401 to your computer and use it in GitHub Desktop.
Get A Number In Prime Series By Index; The nTH Prime.
This file contains 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 | |
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