-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSieve-of-Eratosethenes.cpp
More file actions
78 lines (59 loc) · 1.11 KB
/
Sieve-of-Eratosethenes.cpp
File metadata and controls
78 lines (59 loc) · 1.11 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
// Prime numbers using Sieve of Eratosthenes
#include <iostream>
using namespace std;
void primeSieve(int n){
int count=0;
int prime[100]={0};
for(int i=2; i<=n; i++){
if(prime[i]==0){
for(int j=i*i; j<=n; j+=i){
prime[j]=1;
}
}
}
for(int i=2; i<=n; i++){
if(prime[i]==0){
count++;
cout<<i<<" ";
}
}
cout<<endl;
cout<<"Total numbers of prime number is :";
cout<<count<<endl;
}
int main()
{
int n;
cin>>n;
primeSieve(n);
return 0;
}
// Prime factorisation using Sieve
#include <iostream>
using namespace std;
void primeFactor(int n){
int spf[100]={0};
for(int i=2; i<=n; i++){
spf[i]=i;
}
for(int i=2; i<=n; i++){
if(spf[i]==i){
for(int j=i*i; j<=n; j+=i){
if(spf[j]==j){
spf[j]=i;
}
}
}
}
while(n!=1){
cout<<spf[n]<<" ";
n=n/spf[n];
}
}
int main()
{
int n;
cin>>n;
primeFactor(n);
return 0;
}