-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSieve Of Eratosthenes.cpp
53 lines (49 loc) · 1.1 KB
/
Sieve Of Eratosthenes.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <cstdio>
using namespace std;
#define N 10000000
#define sf scanf
#define pf printf
int isPrime[N+2];
void sieveOfEratosthenes() {
int i, j;
isPrime[0] = isPrime[1] = 1;
for(i=4; i<=N; i+=2) {
isPrime[i] = 1;
}
for(i=3; i*i<=N; i+=2) {
if(!isPrime[i]) {
for(j=i*i; j<=N; j+=i*2) {
isPrime[j] = 1;
}
}
}
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("Primes[1].txt", "w", stdout);
long long int x, i, count=0;
sieveOfEratosthenes();/*
for(i=1; i<=N; i++) {
if(!isPrime[i]) {
cout << i << endl;
}
}
/**/
for(i=1; i<=N; i++) {
if(isPrime[i]==0) {
count++;
}
}
printf("Count: %lld\n", count);/**/
while(sf("%lld", &x)==1 && x) {
if(isPrime[x]==0) {
pf("%lld is a prime number.\n", x);
}else {
pf("%lld is a not prime number.\n", x);
}
}/**/
//fclose(stdin);
//fclose(stdout);
}