Thread: Sieve of Eratosthenes

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Eman View Post
    thanks guys for replying..obviously I have to read up on the stl containers..
    and I also have to learn how to understand and implement efficient algorithms, that's what I want to know..how can I make a program, fast.

    if you know of any good books that may help me, that would great.
    Thanks nimitzhunter and iMalc.
    Before you worry about making it faster/smaller/smarter/prettier ... the first thing is to get it working *at all*... the rest qualifies as improvement.

    Generally what happens is you will notice certain things about your program while it's running and, of course being the programmer you know where to look in your code... so you go in and do little fixups... Over time you will achieve your goals.

    This is what kills me about the RAD crowd... they have tools and code blobs to assemble almost any program in a matter of a couple of days. But lets get real... how much testing and improvement do you think goes into that code... They are, essentially unleashing untested code onto their bosses and customers...

  2. #17
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by CommonTater View Post
    Before you worry about making it faster/smaller/smarter/prettier ... the first thing is to get it working *at all*... the rest qualifies as improvement.
    I did get the program working, just that it works fast for numbers below 100,000, but "fast" is actually slow..

    This is what kills me about the RAD crowd... they have tools and code blobs to assemble almost any program in a matter of a couple of days. But lets get real... how much testing and improvement do you think goes into that code... They are, essentially unleashing untested code onto their bosses and customers...
    who are the RAD?
    I just want to understand how to read algorithms..
    this was my first pathetic attempt, and it didn't cross my mind to use 1's and 0's as another method..ah well
    You ended that sentence with a preposition...Bastard!

  3. #18
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Notice how you skip over items that are not a multiple of i (marked with a comment above)? For a vector you don't have to skip anything! You index straight to and mark off exactly every multiple of i.
    Am I still not skipping when I am checking if sieve[i] is 1 or 0?
    Last edited by Eman; 01-25-2011 at 06:37 AM.
    You ended that sentence with a preposition...Bastard!

  4. #19
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Eman View Post
    Am I still not skipping when I am checking if sieve[i] is 1 or 0?
    Well you could call it that, but the point is that no time is taken to skip over items, you just index straight to the item and mark it off. There could be a million entries between items and it would take the same amount of time as when they were two apart.
    With the list approach you had, an it++ was required to move to the next item and when you're marking off multiples of 97 say, that's quite a lot of it++ that goes on.
    Granted you've already removed some of the items that you would otherwise have to skip over, but that in itself is rather slow as well due to the modulus.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Eman View Post
    who are the RAD?
    Rapid Application Design... the art of distributing essentially untested software.

    I just want to understand how to read algorithms..
    this was my first pathetic attempt, and it didn't cross my mind to use 1's and 0's as another method..ah well
    Not to worry... just keep right on trying.

    Be like edison... "No, I do not know how to make a lightbulb, however I have found many many ways NOT to make a lightbulb."

    It's part of how we learn.

  6. #21
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    I just want to understand how to read algorithms..
    I would recommend to get a good book on algorithm and start reading it. You'll get some referene on where to start. I like the CLR book. It may be a pain in the ass book to carry around though, but very thorough.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  7. #22
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by iMalc View Post
    With the list approach you had, an it++ was required to move to the next item and when you're marking off multiples of 97 say, that's quite a lot of it++ that goes on.
    this is what I don't understand, there is a lot of it++ that goes on..
    but in the recent prog i have
    j+=2, I can see why moving in twos will make a bit of difference, but I still use modulus once as in the first attempt.

    I don't understand why it++ is faster than j+=2, except that I skip one element..

    to my inexperienced viewpoint I would say it was the erase operation that makes it slow..but I thought there would be less to traverse by deleting the item...
    i didn't know the modulus was slow to use...is it not like any other operator += + - etc..
    You ended that sentence with a preposition...Bastard!

  8. #23
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by CommonTater View Post
    Rapid Application Design... the art of distributing essentially untested software.
    Ah, yeah I see.

    Not to worry... just keep right on trying.

    Be like edison... "No, I do not know how to make a lightbulb, however I have found many many ways NOT to make a lightbulb."
    xD good quote (you appear to have a lot of them)..thanks will keep it in mind

    I would recommend to get a good book on algorithm and start reading it. You'll get some referene on where to start. I like the CLR book. It may be a pain in the ass book to carry around though, but very thorough.
    yeah, as soon as I resume college I will look for a good algo book...
    However, the ones I have found are rather...scary, very scary.
    You ended that sentence with a preposition...Bastard!

  9. #24
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    xD good quote (you appear to have a lot of them)..thanks will keep it in mind
    I have not failed. I've just found 10,000 ways that won't work. - Edison

    However, the ones I have found are rather...scary, very scary.
    An algorithm book is an excellent reference tool which you will use when you need to use that and don't remember how. Or, you need to use something and have no ideas. Programming is not really creative for the majority of practitioners.

    As for me, I do not seek to remember everything. To pick an area I try to know well: searching and sorting, since it is such a common problem. But many areas of study are bigger than just algorithms, because algorithms operate on data structures. And the real wizardry in software design is figuring out what is best to use when. A perfect example being today, you found out that your favorite data structure, the linked list, actually has drawbacks. An array like structure, vector, has drawbacks too. And many times in your programming future will you be converting to one or the other to take advantage of some performance-related boon.

  10. #25
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Eman View Post
    this is what I don't understand, there is a lot of it++ that goes on..
    but in the recent prog i have
    j+=2, I can see why moving in twos will make a bit of difference, but I still use modulus once as in the first attempt.

    I don't understand why it++ is faster than j+=2, except that I skip one element..

    to my inexperienced viewpoint I would say it was the erase operation that makes it slow..but I thought there would be less to traverse by deleting the item...
    i didn't know the modulus was slow to use...is it not like any other operator += + - etc..
    There is no +=2 step in the Sieve of Eratosthenes algorithm. It steps by a prime number in size, using each prime number up to sqrt(max). When that prime number is say 97, one could say that it does j+=97 in the array version. The list version by constrast would have to do ++it a lot of times, just to do the equivalent of j+=97.
    Then think about what happens when the prime gets to say 457. The array version goes j+=457 and it's immediately at the next number to mark off. The list version has to step even more times than it did previously, just to step over 457 items (only some of which have been deleted) and then it finally gets to an item to mark off, or in your case - to delete. Worse still, to work out if it has stepped far enough yet, it does a modulus, which yes is slow.

    The modulus operation is the same speed as division, and just as it is slower for a human to do division by hand than say addition, it is slower for a computer as well. A division circuit is much more complicated that an addition circuit because it's a much more complicated process.
    In general, the longer it takes to solve an equation by hand, the longer it takes a computer.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #26
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by iMalc View Post
    There is no +=2 step in the Sieve of Eratosthenes algorithm. It steps by a prime number in size, using each prime number up to sqrt(max). When that prime number is say 97, one could say that it does j+=97 in the array version. The list version by constrast would have to do ++it a lot of times, just to do the equivalent of j+=97.
    Then think about what happens when the prime gets to say 457. The array version goes j+=457 and it's immediately at the next number to mark off. The list version has to step even more times than it did previously, just to step over 457 items (only some of which have been deleted) and then it finally gets to an item to mark off, or in your case - to delete. Worse still, to work out if it has stepped far enough yet, it does a modulus, which yes is slow.
    I would understand what you mean if the way I did it is that way..I now understand what you mean by go straight to the index and mark it off. I didn't do that..I will try that again.. I didn't mean 2's..i meant add it by the amount of the current prime...nimitzhunter's algorithm
    so if j was 3 j+=3 and so on..
    Code:
    #include <iostream>
    #include <list>
    #include <vector>
    #include <stdint.h>
    using namespace std ; 
    #define SIZE 3000000
    int main()
    {
    	vector<int>  sieve(SIZE,1) ;
    	list<int>::iterator it ;
    	list<int> aSieve ;
    	
    
    	sieve[0] =0;
    	sieve[1] = 0;
    		
           int j=1 ;
           unsigned long int sum =0;
           int i =2;
    
           while(i<SIZE)
           {
    			   
    	
            	if(sieve[i])
    	        {
    				
    		        aSieve.push_back(i) ;
    			for(j=i;j<SIZE;j+=i)
    			{
    				sieve[j]=0;
    			}	
    		}
    		i++ ;
    		
          } 
    
    
           it = aSieve.begin() ; 
           while(it!=aSieve.end())
           {
               sum+=*it ;
           
               it++ ;
            }
    
            cout << sum ; 
            cin.get() ; 
            return 0 ;
    }
    In general, the longer it takes to solve an equation by hand, the longer it takes a computer.
    funny, it's supposed to be the other way round..but i get you.
    Last edited by Eman; 01-26-2011 at 05:19 AM.
    You ended that sentence with a preposition...Bastard!

  12. #27
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by whiteflags View Post
    I have not failed. I've just found 10,000 ways that won't work. - Edison
    is that not similar to the lightbulb quote? interesting. Edison has some good quotes..

    An algorithm book is an excellent reference tool which you will use when you need to use that and don't remember how. Or, you need to use something and have no ideas. Programming is not really creative for the majority of practitioners.

    As for me, I do not seek to remember everything. To pick an area I try to know well: searching and sorting, since it is such a common problem. But many areas of study are bigger than just algorithms, because algorithms operate on data structures. And the real wizardry in software design is figuring out what is best to use when. A perfect example being today, you found out that your favorite data structure, the linked list, actually has drawbacks. An array like structure, vector, has drawbacks too. And many times in your programming future will you be converting to one or the other to take advantage of some performance-related boon.
    Yeah, but how would I know which one to use?
    by the way is finding efficient algorithms not the point today and how to use them?
    You ended that sentence with a preposition...Bastard!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sieve of Eratosthenes
    By darren78 in forum C++ Programming
    Replies: 0
    Last Post: 04-28-2010, 10:52 AM
  2. Sieve of Eratosthenes
    By jack_carver in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2010, 10:33 PM
  3. Replies: 2
    Last Post: 12-24-2009, 02:41 AM
  4. Primes with Sieve of Eratosthenes.
    By opciok95 in forum C Programming
    Replies: 20
    Last Post: 11-15-2005, 11:03 AM
  5. sieve of Erotosthenes (HELP!!!)
    By JFK in forum C Programming
    Replies: 1
    Last Post: 02-19-2002, 12:53 PM