finding nth prime number

Nov 15, 2017 at 11:24pm
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 Nov 16, 2017 at 12:15am
Nov 15, 2017 at 11:44pm
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 Nov 15, 2017 at 11:46pm
Topic archived. No new replies allowed.