Thread: Sieve of Eratosthenes

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    Sieve of Eratosthenes

    Code:
    #include <iostream>
    #include <list>
    using namespace std ; 
    #define SIZE 10
    int main()
    {
        list<unsigned long int> sieve ;
        
        list<unsigned long int>::iterator it ; 
        long int i =2 ;
        
        while(i<SIZE)
        {
            sieve.push_back(i) ; 
            i++ ; 
        }
         
        it = sieve.begin() ;
        i=*it;
        
    
        unsigned long int sum =0;
        
        while(i*i < SIZE)
        {
            i = *it ;
            
            sieve.push_back(*it) ;
    	
    	it = sieve.erase(it) ;
            
    		
            while(it!=sieve.end())
            {
               if(i!=*it&&(*it)%i==0)
               {
                
                    it = sieve.erase(it) ; 
                   
                }
                else
                { 
    		 it++ ;
    		
                 }
          
            }
       
    	it = sieve.begin() ;
        } 
    	
        it = sieve.begin() ; 
      
    	
        while(it!=sieve.end())
        {
    	sum+=*it ;
           
            it++ ;
        }
    
        
        cout << "Sum of all prime is: " << sum ;
        cin.get() ; 
        return 0 ;
    }
    this is attempt for sieve of Eras algorithm...but it works freaking slow for numbers above 10000..i was wondering is there a way to make it faster (I am yet to try the Euler sieve algorithm..but I don't see how it is any different)? I don't think it would make any difference if i used vectors..and i don't know how to use vectors anyway.

    One more thing..when I remove the multiples from a list and i increment the iterator, the program crashes i had to use an else for that, to increment the iterator if an element is not erased.

    I don't understand why it crashes, i mean
    i would safely assume that when I removed from a list and incremented the list thereafter the iterator would point to valid address if there was an element after it. But since I can't be sure, it is better to use the else to increment the iterator if not erased... :S
    Last edited by Eman; 01-24-2011 at 08:59 AM.
    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