Thread: Very simple question

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    9

    Very simple question

    I am trying to write a program that stores all the prime numbers up to 1000 in an array.. I can do that.. but when it's doing the check to see if it's prime, if it isn't, it just sets it to 0. So when I try to print out all my prime numbers, it prints all the 0s out with it. How do I remove the 0s from the array? Or what's a better way to do this..
    Last edited by Lej1; 03-09-2012 at 08:20 PM.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Don't print it if the value in the array is 0? Alternatively, don't store it in the array if the it's not prime?

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    The first suggestion works, however, how would I not store it in the array? (with continuing the check)


    Here's your first suggestion:

    Code:
        for(i = 0; i < size; i++)
        {
    	if (arr[i]!=0)
    		printf("%d ",arr[i]);
        }
    Works great!.. what would be more efficient though?
    Last edited by Lej1; 03-09-2012 at 08:15 PM.

  4. #4
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    The sieve of Eratosthenes is considered the correct design pattern for finding prime numbers up to n. Wiki it.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    Yes javaeyes.. the code I wrote is doing just that. But they still have to be stored regardless..

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. The Sieve only stores primes, and then uses the list of primes to check new numbers to see if they are primes.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    Only "stores" the primes? .. So let's look at it like this. I am going to write a program to find all the primes up to 6. (1 will be considered prime.)

    So, I make an empty array with how many spots to store primes?

  8. #8
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    Oh, nice. I was on a kick with c and prime numbers for a long time. I was trying to find a function to produces them, or at least a function that accepts a prime and returns a prime. You can burn ALOT of time trying to find such a function. And it probably doesn't even exist, now that is abstract and thankless hacking. Why are you doing this?

  9. #9
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    I though you only had to check up to the sqrt of the n not all the way, for TSOFE, BUT ur code is gone. I forget what you had. cheers.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Lej1 View Post
    (1 will be considered prime.)
    Why? 1 is not prime.

    Quote Originally Posted by Lej1 View Post
    So, I make an empty array with how many spots to store primes?
    There are 168 primes less than 1000.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want to store the "prime numbers up to 1000", then certainly you only need an array with as many elements as there are "prime numbers up to 1000": these elements will be the prime numbers themselves. But if you want to use the sieve of Eratosthenes to find the "prime numbers up to 1000", then you need an array of about 1000 elements: these elements will be boolean flags that represent whether the corresponding number has been determined to be composite. Those elements that have not been determined to be composite after the algorithm is done are prime.

    Quote Originally Posted by quzah
    No. The Sieve only stores primes, and then uses the list of primes to check new numbers to see if they are primes.
    That sounds more like brute force primality testing with prime numbers.

    Quote Originally Posted by javaeyes
    I though you only had to check up to the sqrt of the n not all the way, for TSOFE
    That sounds more like brute force primality testing.
    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

  12. #12
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    I'm rereading the sieve, you only need to check up to i/2, as the sieve begins with 2. It's odd to think that there in no mathematical function to determine whether a number is prime, nor a function to produce primes. Only algorithms. That's my 5:30 AM thought of the morning.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by javaeyes View Post
    I'm rereading the sieve, you only need to check up to i/2, as the sieve begins with 2.
    Actually, it is only necessary to check to the square root of i, and the reason has nothing to do with "sieve begins with 2".

    Think about it. If x has a factor that exceeds the square root of x then it also has a factor that is less than square root of x.

    However, it is a good idea to avoid using sqrt() from <math.h> to compute the square root of an integer. There are algorithms for finding the integer square root.

    Quote Originally Posted by javaeyes View Post
    It's odd to think that there in no mathematical function to determine whether a number is prime, nor a function to produce primes. Only algorithms. That's my 5:30 AM thought of the morning.
    All mathematical functions execute algorithms. The implementation of that algorithm may be either in code or invoked by specialised machine instructions.

    There are lots of problems with various choices of algorithm (each choice having different computational trade-offs) and for which the best choice of algorithm depends on needs of the program. Testing primality is one of those. So is generating primes.

    Which basically means that, if you want a function to test for primality, you need to write a function that executes some appropriate algorithm.
    Last edited by grumpy; 03-10-2012 at 04:41 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    Ok, whomever can write code that can count the number of primes up to a googol and execute in the shortest amount of time wins the Javaeyes optimized sieve of Eratosthenes to a googol Prize. The JOSOETAGP. Under one minute the prize is a coffee mug. Under one second, the mug is full of beer.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It's easy to get an approximation on the count. For large n (although it is debatable whether a googol is big enough to quality as "large") it was shown mathematically in the 19th century that the number of primes less than or equal to n approaches n/log(n). That means the number of primes less than a googol is approximately 10^98.

    The Sieve of Eratosthenes is reasonably efficient for small primes (say up to a few million or maybe even a billion). Good luck using it to count or find primes up to a googol under a minute on any real-world computer (with a finite processor speed and finite memory).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple (i think) question
    By benji012e in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 12:16 AM
  2. simple simple design question
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2005, 11:33 PM
  3. Simple Question
    By Jackmar in forum C++ Programming
    Replies: 3
    Last Post: 03-18-2003, 01:53 PM
  4. Simple question
    By joebudden in forum C++ Programming
    Replies: 3
    Last Post: 01-06-2003, 06:29 PM
  5. a simple question
    By asim0s in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2002, 02:12 AM