Euler Problem 7 (finding the 10001st prime) in Powershell Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Sieve of Eratosthenes: making it quickerProject Euler Problem 7: Find 10001st prime numberProject Euler #7 - 10001st primeProject Euler #7: 10001st primeFinding the 10001st primeProject Euler Q7 - 10001st primeFinding the 10001st prime in C#Project Euler #7 10001st primeProject Euler Problem #7 in Python (10001st prime number)Euler #7 - 10001st prime
How to make triangles with rounded sides and corners? (squircle with 3 sides)
What does Sonny Burch mean by, "S.H.I.E.L.D. and HYDRA don't even exist anymore"?
One-one communication
Twin's vs. Twins'
Found this skink in my tomato plant bucket. Is he trapped? Or could he leave if he wanted?
calculator's angle answer for trig ratios that can work in more than 1 quadrant on the unit circle
The test team as an enemy of development? And how can this be avoided?
Why does BitLocker not use RSA?
Marquee sign letters
Can gravitational waves pass through a black hole?
Besides transaction validation, are there any other uses of the Script language in Bitcoin
Dinosaur Word Search, Letter Solve, and Unscramble
Getting representations of the Lie group out of representations of its Lie algebra
Was the pager message from Nick Fury to Captain Marvel unnecessary?
What is a more techy Technical Writer job title that isn't cutesy or confusing?
Why can't fire hurt Daenerys but it did to Jon Snow in season 1?
Pointing to problems without suggesting solutions
How do I say "this must not happen"?
How do you write "wild blueberries flavored"?
Plotting a Maclaurin series
Why do the Z-fighters hide their power?
Is the Mordenkainen's Sword spell underpowered?
.bashrc alias for a command with fixed second parameter
What is the proper term for etching or digging of wall to hide conduit of cables
Euler Problem 7 (finding the 10001st prime) in Powershell
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Sieve of Eratosthenes: making it quickerProject Euler Problem 7: Find 10001st prime numberProject Euler #7 - 10001st primeProject Euler #7: 10001st primeFinding the 10001st primeProject Euler Q7 - 10001st primeFinding the 10001st prime in C#Project Euler #7 10001st primeProject Euler Problem #7 in Python (10001st prime number)Euler #7 - 10001st prime
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
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
performance programming-challenge primes powershell
$endgroup$
add a comment |
$begingroup$
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
performance programming-challenge primes powershell
$endgroup$
add a comment |
$begingroup$
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
performance programming-challenge primes powershell
$endgroup$
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
performance programming-challenge primes powershell
performance programming-challenge primes powershell
edited Nov 8 '18 at 18:14
200_success
131k17157422
131k17157422
asked Nov 8 '18 at 18:12
NebulaCodingNebulaCoding
284
284
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
I'd start with
- removing redundancy
- measuring the time automatically
- output to screen slows down, comment it out
depending on the speed of the computer used I get results of ~74..120 seconds.
Compared with 0.450 secs here with matlalb there IS potential to get better ;-)
## Q:Test2018118CR_207250.ps1
$StartTime = get-date
$PrimeNo = 10001
$incNum1 = 0
$highestNum = 0
$k = 1
while($k -lt $PrimeNo)
$incNum2 = 2
$divisNum = 2 * $incNum2 - 1
$incNum1++
$nextNum = 2 * $incNum1 + 1
$upTo = [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
# "0:1" -f $k,$nextNum
$k++
"## PrimeNo: 0 is 1 calculated in 2 seconds" -f $PrimeNo,$highestNum,
((Get-Date)-$StartTime).TotalSeconds
## PrimeNo: 10001 is 104743 calculated in 73,6911884 seconds
## PrimeNo: 10001 is 104743 calculated in 118,3481173 seconds
$endgroup$
$begingroup$
Alright, are there resources you can point me to that could help me improve my algorithm? I already have cut all even numbers from both test pools, and also cut the number of division checks by half. Is this going to require a complete overhaul, or am I just simply missing a few more parts?
$endgroup$
– NebulaCoding
Nov 9 '18 at 13:27
2
$begingroup$
Sorry, I'm too busy with other things ATM to get in deeper. I saw a japanese link translated via google that used the sieve of erasthotenes with a bit array - but the code got mangled by the translation. I think that code was along the lines of this c#-answer on SO. Embedding c# code in PowerShell is not an option?
$endgroup$
– LotPings
Nov 9 '18 at 14:03
$begingroup$
It might be, I just don't have experience with C#. I'll look into implementing the sieve in PS, I've seen it brought up a few times.
$endgroup$
– NebulaCoding
Nov 9 '18 at 14:15
1
$begingroup$
After 2 and 3, all other prime numbers are of the form 6n ± 1, which will speed up your search. For the fastest time you probably need the Sieve of Eratosthenes. According to the Prime Number Theorem, your sieve will need to extend up to about 110,000.
$endgroup$
– rossum
Nov 21 '18 at 19:16
add a comment |
$begingroup$
This get down to around 4 seconds on my 2010 pc.
function Get-nthPrime ([int] $nthPrime )
$approximateMax = $nthPrime * ([math]::Log($nthPrime) + 2) #https://en.wikipedia.org/wiki/Prime_number_theorem
$isprime = @ #hash to hold values
$primeCounter = 0 #count primes we've found
2..$approximateMax
Usage
PS C:> Get-NthPrime 10001
104743
New contributor
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207250%2feuler-problem-7-finding-the-10001st-prime-in-powershell%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
I'd start with
- removing redundancy
- measuring the time automatically
- output to screen slows down, comment it out
depending on the speed of the computer used I get results of ~74..120 seconds.
Compared with 0.450 secs here with matlalb there IS potential to get better ;-)
## Q:Test2018118CR_207250.ps1
$StartTime = get-date
$PrimeNo = 10001
$incNum1 = 0
$highestNum = 0
$k = 1
while($k -lt $PrimeNo)
$incNum2 = 2
$divisNum = 2 * $incNum2 - 1
$incNum1++
$nextNum = 2 * $incNum1 + 1
$upTo = [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
# "0:1" -f $k,$nextNum
$k++
"## PrimeNo: 0 is 1 calculated in 2 seconds" -f $PrimeNo,$highestNum,
((Get-Date)-$StartTime).TotalSeconds
## PrimeNo: 10001 is 104743 calculated in 73,6911884 seconds
## PrimeNo: 10001 is 104743 calculated in 118,3481173 seconds
$endgroup$
$begingroup$
Alright, are there resources you can point me to that could help me improve my algorithm? I already have cut all even numbers from both test pools, and also cut the number of division checks by half. Is this going to require a complete overhaul, or am I just simply missing a few more parts?
$endgroup$
– NebulaCoding
Nov 9 '18 at 13:27
2
$begingroup$
Sorry, I'm too busy with other things ATM to get in deeper. I saw a japanese link translated via google that used the sieve of erasthotenes with a bit array - but the code got mangled by the translation. I think that code was along the lines of this c#-answer on SO. Embedding c# code in PowerShell is not an option?
$endgroup$
– LotPings
Nov 9 '18 at 14:03
$begingroup$
It might be, I just don't have experience with C#. I'll look into implementing the sieve in PS, I've seen it brought up a few times.
$endgroup$
– NebulaCoding
Nov 9 '18 at 14:15
1
$begingroup$
After 2 and 3, all other prime numbers are of the form 6n ± 1, which will speed up your search. For the fastest time you probably need the Sieve of Eratosthenes. According to the Prime Number Theorem, your sieve will need to extend up to about 110,000.
$endgroup$
– rossum
Nov 21 '18 at 19:16
add a comment |
$begingroup$
I'd start with
- removing redundancy
- measuring the time automatically
- output to screen slows down, comment it out
depending on the speed of the computer used I get results of ~74..120 seconds.
Compared with 0.450 secs here with matlalb there IS potential to get better ;-)
## Q:Test2018118CR_207250.ps1
$StartTime = get-date
$PrimeNo = 10001
$incNum1 = 0
$highestNum = 0
$k = 1
while($k -lt $PrimeNo)
$incNum2 = 2
$divisNum = 2 * $incNum2 - 1
$incNum1++
$nextNum = 2 * $incNum1 + 1
$upTo = [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
# "0:1" -f $k,$nextNum
$k++
"## PrimeNo: 0 is 1 calculated in 2 seconds" -f $PrimeNo,$highestNum,
((Get-Date)-$StartTime).TotalSeconds
## PrimeNo: 10001 is 104743 calculated in 73,6911884 seconds
## PrimeNo: 10001 is 104743 calculated in 118,3481173 seconds
$endgroup$
$begingroup$
Alright, are there resources you can point me to that could help me improve my algorithm? I already have cut all even numbers from both test pools, and also cut the number of division checks by half. Is this going to require a complete overhaul, or am I just simply missing a few more parts?
$endgroup$
– NebulaCoding
Nov 9 '18 at 13:27
2
$begingroup$
Sorry, I'm too busy with other things ATM to get in deeper. I saw a japanese link translated via google that used the sieve of erasthotenes with a bit array - but the code got mangled by the translation. I think that code was along the lines of this c#-answer on SO. Embedding c# code in PowerShell is not an option?
$endgroup$
– LotPings
Nov 9 '18 at 14:03
$begingroup$
It might be, I just don't have experience with C#. I'll look into implementing the sieve in PS, I've seen it brought up a few times.
$endgroup$
– NebulaCoding
Nov 9 '18 at 14:15
1
$begingroup$
After 2 and 3, all other prime numbers are of the form 6n ± 1, which will speed up your search. For the fastest time you probably need the Sieve of Eratosthenes. According to the Prime Number Theorem, your sieve will need to extend up to about 110,000.
$endgroup$
– rossum
Nov 21 '18 at 19:16
add a comment |
$begingroup$
I'd start with
- removing redundancy
- measuring the time automatically
- output to screen slows down, comment it out
depending on the speed of the computer used I get results of ~74..120 seconds.
Compared with 0.450 secs here with matlalb there IS potential to get better ;-)
## Q:Test2018118CR_207250.ps1
$StartTime = get-date
$PrimeNo = 10001
$incNum1 = 0
$highestNum = 0
$k = 1
while($k -lt $PrimeNo)
$incNum2 = 2
$divisNum = 2 * $incNum2 - 1
$incNum1++
$nextNum = 2 * $incNum1 + 1
$upTo = [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
# "0:1" -f $k,$nextNum
$k++
"## PrimeNo: 0 is 1 calculated in 2 seconds" -f $PrimeNo,$highestNum,
((Get-Date)-$StartTime).TotalSeconds
## PrimeNo: 10001 is 104743 calculated in 73,6911884 seconds
## PrimeNo: 10001 is 104743 calculated in 118,3481173 seconds
$endgroup$
I'd start with
- removing redundancy
- measuring the time automatically
- output to screen slows down, comment it out
depending on the speed of the computer used I get results of ~74..120 seconds.
Compared with 0.450 secs here with matlalb there IS potential to get better ;-)
## Q:Test2018118CR_207250.ps1
$StartTime = get-date
$PrimeNo = 10001
$incNum1 = 0
$highestNum = 0
$k = 1
while($k -lt $PrimeNo)
$incNum2 = 2
$divisNum = 2 * $incNum2 - 1
$incNum1++
$nextNum = 2 * $incNum1 + 1
$upTo = [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
# "0:1" -f $k,$nextNum
$k++
"## PrimeNo: 0 is 1 calculated in 2 seconds" -f $PrimeNo,$highestNum,
((Get-Date)-$StartTime).TotalSeconds
## PrimeNo: 10001 is 104743 calculated in 73,6911884 seconds
## PrimeNo: 10001 is 104743 calculated in 118,3481173 seconds
answered Nov 8 '18 at 21:05
LotPingsLotPings
2467
2467
$begingroup$
Alright, are there resources you can point me to that could help me improve my algorithm? I already have cut all even numbers from both test pools, and also cut the number of division checks by half. Is this going to require a complete overhaul, or am I just simply missing a few more parts?
$endgroup$
– NebulaCoding
Nov 9 '18 at 13:27
2
$begingroup$
Sorry, I'm too busy with other things ATM to get in deeper. I saw a japanese link translated via google that used the sieve of erasthotenes with a bit array - but the code got mangled by the translation. I think that code was along the lines of this c#-answer on SO. Embedding c# code in PowerShell is not an option?
$endgroup$
– LotPings
Nov 9 '18 at 14:03
$begingroup$
It might be, I just don't have experience with C#. I'll look into implementing the sieve in PS, I've seen it brought up a few times.
$endgroup$
– NebulaCoding
Nov 9 '18 at 14:15
1
$begingroup$
After 2 and 3, all other prime numbers are of the form 6n ± 1, which will speed up your search. For the fastest time you probably need the Sieve of Eratosthenes. According to the Prime Number Theorem, your sieve will need to extend up to about 110,000.
$endgroup$
– rossum
Nov 21 '18 at 19:16
add a comment |
$begingroup$
Alright, are there resources you can point me to that could help me improve my algorithm? I already have cut all even numbers from both test pools, and also cut the number of division checks by half. Is this going to require a complete overhaul, or am I just simply missing a few more parts?
$endgroup$
– NebulaCoding
Nov 9 '18 at 13:27
2
$begingroup$
Sorry, I'm too busy with other things ATM to get in deeper. I saw a japanese link translated via google that used the sieve of erasthotenes with a bit array - but the code got mangled by the translation. I think that code was along the lines of this c#-answer on SO. Embedding c# code in PowerShell is not an option?
$endgroup$
– LotPings
Nov 9 '18 at 14:03
$begingroup$
It might be, I just don't have experience with C#. I'll look into implementing the sieve in PS, I've seen it brought up a few times.
$endgroup$
– NebulaCoding
Nov 9 '18 at 14:15
1
$begingroup$
After 2 and 3, all other prime numbers are of the form 6n ± 1, which will speed up your search. For the fastest time you probably need the Sieve of Eratosthenes. According to the Prime Number Theorem, your sieve will need to extend up to about 110,000.
$endgroup$
– rossum
Nov 21 '18 at 19:16
$begingroup$
Alright, are there resources you can point me to that could help me improve my algorithm? I already have cut all even numbers from both test pools, and also cut the number of division checks by half. Is this going to require a complete overhaul, or am I just simply missing a few more parts?
$endgroup$
– NebulaCoding
Nov 9 '18 at 13:27
$begingroup$
Alright, are there resources you can point me to that could help me improve my algorithm? I already have cut all even numbers from both test pools, and also cut the number of division checks by half. Is this going to require a complete overhaul, or am I just simply missing a few more parts?
$endgroup$
– NebulaCoding
Nov 9 '18 at 13:27
2
2
$begingroup$
Sorry, I'm too busy with other things ATM to get in deeper. I saw a japanese link translated via google that used the sieve of erasthotenes with a bit array - but the code got mangled by the translation. I think that code was along the lines of this c#-answer on SO. Embedding c# code in PowerShell is not an option?
$endgroup$
– LotPings
Nov 9 '18 at 14:03
$begingroup$
Sorry, I'm too busy with other things ATM to get in deeper. I saw a japanese link translated via google that used the sieve of erasthotenes with a bit array - but the code got mangled by the translation. I think that code was along the lines of this c#-answer on SO. Embedding c# code in PowerShell is not an option?
$endgroup$
– LotPings
Nov 9 '18 at 14:03
$begingroup$
It might be, I just don't have experience with C#. I'll look into implementing the sieve in PS, I've seen it brought up a few times.
$endgroup$
– NebulaCoding
Nov 9 '18 at 14:15
$begingroup$
It might be, I just don't have experience with C#. I'll look into implementing the sieve in PS, I've seen it brought up a few times.
$endgroup$
– NebulaCoding
Nov 9 '18 at 14:15
1
1
$begingroup$
After 2 and 3, all other prime numbers are of the form 6n ± 1, which will speed up your search. For the fastest time you probably need the Sieve of Eratosthenes. According to the Prime Number Theorem, your sieve will need to extend up to about 110,000.
$endgroup$
– rossum
Nov 21 '18 at 19:16
$begingroup$
After 2 and 3, all other prime numbers are of the form 6n ± 1, which will speed up your search. For the fastest time you probably need the Sieve of Eratosthenes. According to the Prime Number Theorem, your sieve will need to extend up to about 110,000.
$endgroup$
– rossum
Nov 21 '18 at 19:16
add a comment |
$begingroup$
This get down to around 4 seconds on my 2010 pc.
function Get-nthPrime ([int] $nthPrime )
$approximateMax = $nthPrime * ([math]::Log($nthPrime) + 2) #https://en.wikipedia.org/wiki/Prime_number_theorem
$isprime = @ #hash to hold values
$primeCounter = 0 #count primes we've found
2..$approximateMax
Usage
PS C:> Get-NthPrime 10001
104743
New contributor
$endgroup$
add a comment |
$begingroup$
This get down to around 4 seconds on my 2010 pc.
function Get-nthPrime ([int] $nthPrime )
$approximateMax = $nthPrime * ([math]::Log($nthPrime) + 2) #https://en.wikipedia.org/wiki/Prime_number_theorem
$isprime = @ #hash to hold values
$primeCounter = 0 #count primes we've found
2..$approximateMax
Usage
PS C:> Get-NthPrime 10001
104743
New contributor
$endgroup$
add a comment |
$begingroup$
This get down to around 4 seconds on my 2010 pc.
function Get-nthPrime ([int] $nthPrime )
$approximateMax = $nthPrime * ([math]::Log($nthPrime) + 2) #https://en.wikipedia.org/wiki/Prime_number_theorem
$isprime = @ #hash to hold values
$primeCounter = 0 #count primes we've found
2..$approximateMax
Usage
PS C:> Get-NthPrime 10001
104743
New contributor
$endgroup$
This get down to around 4 seconds on my 2010 pc.
function Get-nthPrime ([int] $nthPrime )
$approximateMax = $nthPrime * ([math]::Log($nthPrime) + 2) #https://en.wikipedia.org/wiki/Prime_number_theorem
$isprime = @ #hash to hold values
$primeCounter = 0 #count primes we've found
2..$approximateMax
Usage
PS C:> Get-NthPrime 10001
104743
New contributor
New contributor
answered 1 min ago
StephenPStephenP
1011
1011
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207250%2feuler-problem-7-finding-the-10001st-prime-in-powershell%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown