首页 > 代码库 > Largest prime factor

Largest prime factor

problem 3:Largest prime factor

题意:求600851475143的最大的质因数

代码如下:

 1 #ifndef PRO3_H_INCLUDED
 2 #define PRO3_H_INCLUDED
 3 
 4 #include "prime.h"
 5 
 6 namespace pro3{
 7     long long solve(){
 8         long long n=600851475143LL,maxn=0;;
 9         for(long long i=1;i*i<=n;++i)
10         if(n%i==0){
11             if(isPrime(n/i))return n/i;
12             else if(isPrime(i))maxn=i;
13         }
14         return 1>maxn?1:maxn;
15     }
16 }
17 
18 #endif // PRO3_H_INCLUDED

其中,bool isPrime(long long x)如下(后面不再赘述):

技术分享
1 bool isPrime(long long x){
2     if(x<=1)return 0;
3     for(long long i=2;i*i<=x;++i)
4         if(x%i==0)return 0;
5     return 1;
6 }
bool isPrime(long long x)

 

Largest prime factor