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.