Prime#s

Discussion in 'Programming General' started by chaoticape, Jun 8, 2011.

Prime#s
  1. Unread #1 - Jun 8, 2011 at 9:31 AM
  2. chaoticape
    Joined:
    Jun 7, 2011
    Posts:
    1
    Referrals:
    0
    Sythe Gold:
    0

    chaoticape Newcomer

    Prime#s

    So I just started C++ a few days ago, forgive me if I did anything royally stupid.
    Basically trying to make a small program to give you a list of prime numbers up to a certain number with the Sieve of Eratosthenes algorithm. The problem is that instead of outputting each prime once, they each get spammed for a second.

    Code:
    // prim1to100.cpp : Defines the entry point for the console application.
    // Sieve of Eratosthenes model
    
    #include "stdafx.h"
    #include "../../std_lib_facilities.h"
    
    vector<int>not_prime;
    vector<int>primes;
    bool result;
    void cross_out(int x)
    {
    	int y = x;
    		for(int p = 0;p<100;++p) {			//Crosses out numbers and their multiples and adds them to the not prime vector
    			y += x;
    			not_prime.push_back(y);	
    		}										
    }
    
    void find_primes(int prime)
    {
    	for(int timer = 0; timer < not_prime.size(); ++timer) {//looks for numbers that aren't on the not prime vector and adds them to prime
    		sort(not_prime.begin(), not_prime.end());
    		result = (binary_search(not_prime.begin(), not_prime.end(), prime));
    		if(result == true);
    		else primes.push_back(prime);		
    	}
    }
    		
    
    int main()
    {
    	cross_out(2);
    	cross_out(3);
    	cross_out(5);
    	cross_out(7);
    	for(int timer2 = 0; timer2 < 100; ++timer2) //Should find all numbers not in not_prime vector
    		find_primes(timer2);
    	cout << "All prime numbers to 10,000,000: \n";
    		sort(primes.begin(), primes.end());
    		primes.erase( unique ( primes.begin(), primes.end()));
    		for (int timer3 = 0; timer3 < primes.size(); ++timer3) //Lists all elements of primes vector
    			cout << primes[timer3] << endl;
    	system("pause");
    	return 0;
    }//Fucked up somewhere, doesn't work.
    
     
  3. Unread #2 - Jun 12, 2011 at 12:51 PM
  4. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Prime#s

    If you try to do some debugging and print out the content of "primes" right after your unique() function call you'll see it does have the primes already in order at the beginning of the vector.

    It looks something like this: 012357111317192329313741434753596167717379838997000000000000...1111111111111...2222222222222222...3333333333333... and so on.

    You need to use resize() on your prime vector after the unique() call so that it only contains the number of elements that are unique which are at the beginning of the vector and it will end up looking like this: 012357111317192329313741434753596167717379838997.

    I don't really want to give you the code (you are very close). Have a look here and see if you can figure it out, they're doing something very similar.
    http://www.cplusplus.com/reference/algorithm/unique
     
< How to make it click on a image? | Making a Swiftkit/Eliteswitch Alike development in vb.net >

Users viewing this thread
1 guest


 
 
Adblock breaks this site