sieve of Eratosthenes using vector
This is the best code from my brain. Can you guys give any advice for it? This is far easier than my friend's program. Did I forget something?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include <iostream>
#include <vector>
using namespace std;
int main() {
int i;
cout << "Enter an integer: ";
cin >> i;
vector<int> v;
for (int k = 0; k <i; k ++)
{
v.push_back(k);
}
for (int a = 2; a < 1000; a++)
{
for (int b = 2; a*b < i; b++)
{
v[a*b] = 0;
}
}
for (int c = 0; c < v.size(); c++)
{
if (v[c] != 0 && v[c] != 1)
{
cout << v[c] << " ";
}
}
cout << endl;
}
|
Last edited on
Topic archived. No new replies allowed.