-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrime Factor.cpp
More file actions
81 lines (77 loc) · 1.4 KB
/
Prime Factor.cpp
File metadata and controls
81 lines (77 loc) · 1.4 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
79
80
81
#include<iostream>
#include<math.h>
#define SIZE 1000000
using namespace std;
long p[SIZE],pvalues[SIZE], n;
long list[SIZE];
long listsize;
void prime(void)
{
long i,j;
p[0] = 1;
p[1] = 1;
for(i=2; i<= sqrt(SIZE);i++)
{
if(p[i]==0)
{
for(j=i*i; j <= SIZE; j+=i)
{
p[j] = 1;
}
}
}
j=0;
for(i=0;i<=SIZE;i++)
{
if(p[i]==0)
{
pvalues[j++]=i;
}
}
return;
}
void primeFactorize(long n)
{
listsize = 0;
long sqrtn = long (sqrt(n));
for(long i=0; pvalues[i]<= sqrtn; i++)
{
if(n % pvalues[i] == 0)
{
while(n % pvalues[i]==0)
{
n/= pvalues[i];
list[listsize] = pvalues[i];
listsize++;
}
}
}
if(n > 1)
{
list[listsize] = n;
listsize++;
}
}
int main()
{
prime();
cout<<"Enter the number to factorize: ";
cin>>n;
if(n==1){
cout<<"1=1"<<endl;
}
else if(n == -1){
cout<<"-1 = -1 x 1"<<endl;
}
else
{
primeFactorize(n);
}
cout<<"The number"<<n<<"has the following factors :\n";
for(long i=0; i< listsize;i++)
{
cout<<list[i]<<"\t";
}
cout<<endl;
return 0;
}