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;








4












$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









share|improve this question











$endgroup$


















    4












    $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









    share|improve this question











    $endgroup$














      4












      4








      4





      $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









      share|improve this question











      $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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 '18 at 18:14









      200_success

      131k17157422




      131k17157422










      asked Nov 8 '18 at 18:12









      NebulaCodingNebulaCoding

      284




      284




















          2 Answers
          2






          active

          oldest

          votes


















          1












          $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





          share|improve this answer









          $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


















          0












          $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




          share








          New contributor




          StephenP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          $endgroup$













            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
            );



            );













            draft saved

            draft discarded


















            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









            1












            $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





            share|improve this answer









            $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















            1












            $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





            share|improve this answer









            $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













            1












            1








            1





            $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





            share|improve this answer









            $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






            share|improve this answer












            share|improve this answer



            share|improve this answer










            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
















            • $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













            0












            $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




            share








            New contributor




            StephenP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            $endgroup$

















              0












              $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




              share








              New contributor




              StephenP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






              $endgroup$















                0












                0








                0





                $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




                share








                New contributor




                StephenP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                $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





                share








                New contributor




                StephenP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.








                share


                share






                New contributor




                StephenP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered 1 min ago









                StephenPStephenP

                1011




                1011




                New contributor




                StephenP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                StephenP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                StephenP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.



























                    draft saved

                    draft discarded
















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

                    Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

                    Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar