- #1
CrazyNeutrino
- 100
- 0
I've just started with project Euler. The problem I just finished is phrased as follows:
"The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?"
The method i used was trial division. Here is my code in C:
Although this did solve the problem, (answer is 6572) I have read that you only need to check for factors upto the square root of n. I tried doing this by setting the condition x*x<n instead of x<n. This however, does not work and prints a value of 1472. Can someone explain why?
"The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?"
The method i used was trial division. Here is my code in C:
C:
#include<stdio.h>
int main()
{
long long n = 600851475143;
long long x;
long long factor;
for(x=2; x<n; x++)
{
while( n % x == 0 )
{
n = n / x;
factor = x;
}
}
printf("\n%lld\n", x);
return 0;
}
Although this did solve the problem, (answer is 6572) I have read that you only need to check for factors upto the square root of n. I tried doing this by setting the condition x*x<n instead of x<n. This however, does not work and prints a value of 1472. Can someone explain why?
Last edited by a moderator: