Write a program that finds prime numbers using an a
lgorithm that works as follows. Assume we wish
to determine which numbers between 2 and 100 are pr
ime. Fill an array with 100 1s: initially every
number between 2 and 100 is considered prime. Consi
der 2: it is prime, so cross out every second
number (4, 6, 8, etc.) (set to 0 the corresponding
array entry). Then come back to the beginning of th
e
array and move (from 2) on to the next prime number
, the next one not crossed out: 3. Cross out every
third number: 6, 9, 12, etc. Continue this process,
advancing to the next prime number and crossing
out all its multiples until you exceed 100. At this
point, every number not crossed out is prime. Your
program should print all prime numbers between 2 an
N, where N is a symbolic constant. Make N
equal to 100.
and this is what i tried so far but it gives me infinite loop idk why
i cout it just to make sure its working right, anyone can help?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include<iostream>
using namespace std;
int main() {
bool prime[100];
for (int i=2;i<100;i++)
{prime[i]=1;}
for(int i=2;i<100;i+2)
{prime[i]=0;
cout<<prime[i];}
return 0;
}
|