-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprime_factorization.cpp
More file actions
56 lines (49 loc) · 1011 Bytes
/
prime_factorization.cpp
File metadata and controls
56 lines (49 loc) · 1011 Bytes
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
// Algorithm to find all the prime factors of a number
#include<iostream>
#include<math.h>
using namespace std;
// A function to print all prime factors of a given number n
void primeFactors(int n)
{
// Print the number of 2s that divide n
while (n%2 == 0)
{
cout<<2<<endl;
n = n/2;
}
// n must be odd at this point. So we can skip one element (Note i = i +2)
for (int i = 3; i <= sqrt(n); i = i+2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
cout<<i<<endl;
n = n/i;
}
}
// This condition is to handle the case whien n is a prime number
// greater than 2
if (n > 2)
cout<<n<<endl;
}
void prime_factors(int n){
int p=1;
for(int i=2;i*i<=n;i++){
while(n%i==0){
p=i;
n/=i;
}
cout<<p<<"\n";
}
if(n>1){
cout<<n<<"\n";
}
}
int main()
{
int n;
cout<<"Enter the number:";
cin>>n;
primeFactors(n);
return 0;
}