Thread: Recuration Prime Numbers

  1. #16
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Oh, then store the numbers of found primes and loop till you have found the nth prime.

  2. #17
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    Quote Originally Posted by laserlight View Post
    Yes, that is the basis for my (invalid) submission for a primality testing contest here. Yet, with just 1000 primes, it is feasible to hard code all of them, instead of using a hard coded base of the first 24 primes to generate them all. But megazord, do the rules allow such hard coding in the first place?
    No, the max length of the array that I can give is 256.
    how can I generate all the primes with the first 24?

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by megazord
    No, the max length of the array that I can give is 256.
    how can I generate all the primes with the first 24?
    If the max length of the array is 256, then you cannot "calculate more primes on demand", at least not enough to reach 1000 times and thereby use the incredibly easy solution that I described. It also rules out the sieve of Eratosthenes.

    But what you can do is this: observe that the 1000th prime is 7919. Therefore, all composite numbers less than the 1000th prime have a prime divisor no greater than the square root of the 1000th prime, which is a little less than 89. 89 is the 24th prime (so actually you only need the first 23 primes). So, when you are doing trial division, you only need to test with these 23 primes. If the number does not have a divisor among these, the number must be prime.

    Another thing to do: skip all the even numbers greater than two.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Perfect and Prime numbers
    By taggrath in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2009, 02:13 AM
  2. Finding Prime Numbers
    By dan724 in forum C Programming
    Replies: 11
    Last Post: 12-14-2008, 12:12 PM
  3. Checking very large prime numbers
    By password in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2008, 12:26 PM
  4. Replies: 18
    Last Post: 11-04-2005, 02:41 PM
  5. Replies: 2
    Last Post: 09-11-2002, 05:00 PM