全部博文(68)
分类: C/C++
2011-04-26 22:25:13
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
答案:6857
#include
#include
int main(void)
{
long int n = 600851475143L;
int lastFactor = 2;
if(n%2==0){
lastFactor = 2;
n = n/2;
while(n%2==0)
n = n/2;
}
else
lastFactor = 1;
int factor = 3;
int maxFactor = sqrt(n);
while(n>1 && factor<=maxFactor){
if(n%factor==0){
n = n/factor;
lastFactor = factor;
while(n/factor==0)
n = n/factor;
maxFactor = sqrt(n);
}
factor += 2;
}
if(n==1)
printf("%d\n", lastFactor);
else
printf("%d\n", n);
return 0;
}