forked from tanus786/CP-Codes-HackOctober-Fest-2023
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSeiveOfEratosthenes.cpp
48 lines (42 loc) · 902 Bytes
/
SeiveOfEratosthenes.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
#include<bits/stdc++.h>
using namespace std;
void SeiveOfEratosthenes(int num){
int arr[num+1] = {0};
for(int i=2; i<=num; i++){
if(arr[i]==0){
for(int j=i*i; j<=num; j+=i){
arr[j] = 1;
}
}
}
for(int i=2; i<=num; i++){
if(arr[i]==0)
cout<<i<<' ';
}cout<<endl;
}
void PrimeFactorisation(int num){
int arr[num+1];
for(int i=0; i<=num; i++)
arr[i] = i;
for(int i=2; i<=num; i++){
if(arr[i]==i){
for(int j=i*i; j<=num; j+=i){
if(arr[j]==j){
arr[j] = i;
}
}
}
}
while(num!=1){
cout<<arr[num]<<' ';
num /= arr[num];
}cout<<endl;
}
int main(){
int n;
cout<<"Enter a number: ";
cin>>n;
SeiveOfEratosthenes(n);
PrimeFactorisation(n);
return 0;
}