-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
prime_factors.cpp
55 lines (51 loc) · 1.35 KB
/
prime_factors.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
/* This algorithm will represent a number n(>1) as product of prime numbers.
For example, if the input is 12 then output will be 2 2 3 , if input is 28 then output will be (2,2,7).
Two concepts are used here. Firstly , through sieve of erathosthenes we will find all the prime numbers till n.
Then we will check which prime numbers can divide n and for how many times*/
#include<iostream>
#include <cmath>
using namespace std;
void prime_factors(int n)
{
//Applying sieve of erathosthenes to store the prime numbers till n
int arr[n+1]={0};
arr[1]=1;
for(int i=2;i<=sqrt(n);i++)
{
if(arr[i]==0)
{
for(int j=i*i;j<=n;j+=i)
{
arr[j]=1;
}
}
}
for(int i=1;i<=n;i++)
{
if(arr[i]==0) //checking if i is prime
{
while(n%i==0) //while i divides n, print i and keep on dividing n by i
{
cout<<i<<" ";
n=n/i;
}
}
}
return;
}
//Driver Code
int main()
{
int num;
cin>>num;
prime_factors(num);
return 0;
}
/* sample input: 12
sample output: 2 2 3
sample input: 24
sample output: 2 2 2 3
Time Complexity: O(n*log(log(n)) + log(n) + n)
for sieve of erathrosthenes => O(n*log(log(n)))
for printing the prime factors => O(n+ log(n))
Space Complexity: O(n) */