I am having issue making this code more efficient. The problem to be solved is as follows:
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10,001st prime number?
I'm looking to complete this in powershell. My code runs well up until about 4.8k primes.
$incNum1 = 1
$incNum2 = 2
$divisNum = 2 * $incNum2 - 1
$highestNum = 0
$k = 1
$nextNum = 2 * $incNum1 + 1
while($k -lt 6000){
$upTo = [int][Math]::Ceiling(($nextNum / 2))
$break = $false
while($divisNum -lt $upTo){
$modRes = $nextNum % $divisNum
if($modRes -eq 0){
$break = $true
break
}
$incNum2++
$divisNum = 2 * $incNum2 - 1
}
if(!$break){
$highestNum = $nextNum
echo $nextNum
$k++
}
$incNum2 = 2
$divisNum = 2 * $incNum2 - 1
$incNum1++
$nextNum = 2 * $incNum1 + 1
}
echo $highestNum