finding nth prime number

This program is supposed to be able to output the nth prime number. For example if the user enters "4" the program should output 7 because that is the fourth prime number.
Last edited on
Your IsPrime() isn’t simply unoptimal, it is flat out wrong. (Sorry.)

Consider using the Sieve of Eratosthenes algorithm. That is, build yourself a list of prime numbers: for each new number you wish to add to the list, only add it if none of the numbers currently in the list divide evenly into it; stop when your list is N primes long. (Yes, I’m advocating an n² algorithm.)

PS. You don't need globals.
Last edited on
Topic archived. No new replies allowed.