-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_prime.cpp
66 lines (58 loc) · 1.34 KB
/
print_prime.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
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
// 1] Navie Approach T.C. = O(n^2)
bool is_prime(int n){
if(n == 0 || n == 1)
return 0;
else{
for(int i = 2; i < n; i++){
if(n % i == 0){
return 0;
}
}
return 1;
}
}
// 2] Sqrt Approach T.C. = O(n*sqrt(n))
bool is_prime_01(int n){
if(n == 0 || n == 1)
return 0;
else{
for(int i = 2; i < sqrt(n); i++){
if(n % i == 0){
return 0;
}
}
return 1;
}
}
// 3] Sieve of Eratosthenes T.C. = O(n*(log*(logn)))
int countPrimes(int n) {
if(n == 0 || n == 1) return 0;
vector <bool> prime(n,true); // all are marked as prime already
int count = 0;
prime[0] = prime[1] = false;
for(int i = 2; i < prime.size(); i++){
if(prime[i]){
count++;
int j = 2 * i;
while(j < n){
prime[j] = false;
j += i;
}
}
}
return count;
}
int main()
{
int num;
cout<<"Enter num: ";
cin>>num;
for(int i = 2; i < num; i++)
if(is_prime(i)) cout<<i<<" ";
cout<<endl;
return 0;
}