here is my code in C for problem#3 from project-Euler, where I have to find the largest prime factor of 600851475143.
#include <stdio.h>
#include <stdlib.h>
bool is_prime(long int number){
long int j;
for (j=2; j<=number/2; j++){
if (number%j==0) return false;
if (j==number/2) return true;
}
}
int main(){
long int input;
scanf("%d", &input);
long int factor;
int ans=0;
for (factor=input/2; factor>1; factor--){
if (input%factor==0 && is_prime(factor)) {
ans = factor;
break;
}
}
printf("%d
", ans);
system("pause");
return 0;
}
Although it works fine for small numbers, gradually it takes more and more time for it to give an answer. And, finally, for 600851475143 the code returns 0, which is obviously wrong.
Could anyone help?
thanks a lot.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…