Solved

How to find the Nth prime number

Posted on 2007-11-28
4
5,697 Views
Last Modified: 2010-10-05
I need to find the Nth prime number. I.E. if the input is 3 it should return 5 because the first three prime numbers are 2,3, and 5. 4 should return 7 and so on. I just need an algorithm or some pseudocode here right now because i'm writing this in assembler. So far this is what i have.

for(divisor = 2; divisor < num; divis++)
{
     if((num mod divisor) == 0)
          num is not prime
          break out of the loop
}
if((num - divisor) == 1)
     num is a prime
//if num - divisor == 1 that means that it made it the whole way through the for loop without being broken out of the loop and there was no number that would divide the number therefore it is prime.


     
0
Comment
Question by:toymachiner62
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Expert Comment

by:Hardi
ID: 20371524
0
 
LVL 6

Expert Comment

by:Hardi
ID: 20371548
There is a site for calculating the n-th prime, and it explains a bit of the algorithm you may be interested
http://primes.utm.edu/nthprime/algorithm.php
0
 
LVL 29

Expert Comment

by:pepr
ID: 20371817
I would recommend to have a look at wikipedia "Prime number" (http://en.wikipedia.org/wiki/Prime_number).  You will find there some theory and also a lot of references.

See the section "5.1 Finding prime numbers" (http://en.wikipedia.org/wiki/Prime_number#Finding_prime_numbers). The very first references in the section point to the "easy, brute force" and "fast" algorithms:

Sieve of Eratosthenes (http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)
Sieve of Atkin (http://en.wikipedia.org/wiki/Sieve_of_Atkin -- contains reference to implementation in Python and in C)
0
 
LVL 6

Accepted Solution

by:
Talmash earned 250 total points
ID: 20372965
beyond the info on web:

find = 4 // find first 4 primes
found=1 // still only "2" found
print "2\n"
num = 3 // check if num is a prime

while (found < find) {
  for(divisor = 2; divisor < num; divis++) {
     if((num mod divisor) == 0) {
          num is not prime
          break out of the "for" loop
     }
   
    if(divisor*divisor > num) {
    //if((num - divisor) == 1)
     num is a prime
     print num
     found++
     break out of the "for" loop
    }
  } // close "for"
} // close while

this code is closer to what you need.

tal
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Embarcadero C++ builder XE10.1 Berlin TRegistry declaration 1 60
IDE for Python 5 109
Error building VS2105 solution from repository 1 62
SSIS - Mixed Mode Assembly Error 4 42
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question