-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargestPrimeFactor.cpp
More file actions
37 lines (35 loc) · 859 Bytes
/
largestPrimeFactor.cpp
File metadata and controls
37 lines (35 loc) · 859 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
// Example program
#include <iostream> // essential libraries
#include <string> // essential libraries
using namespace std;
int main(){
bool is_prime( long long int );
long long int n;
long long int x = 0;
cin >>n;
long long int k = n;
if ( is_prime(n) == true ) {
cout<<"The biggest prime factor of "<<n<<" is "<<n<<endl;
}
else {
for ( x = 2; x < k; x++ ){
if ( is_prime(x) == true ){ //check if divisor is prime
while ( k % x == 0 ){ //check if divisor is a factor
k = k/x;
}
}
}
cout<<"The biggest prime factor of given number is "<<x<<endl;
}
}
bool is_prime( long long int n ){
if ( n % 2 == 0 && n > 2){
return false;
}
for( long long int i = 2; i*i < n; i++) {
if( n % i == 0 ){
return false;
}
}
return true;
}