here is my code in C for problem#3 from project-Euler, where I have to find the largest prime factor of 600851475143.
#include
#include
Try the following code. It essentially implements the points in the accepted answer. The only improvement is that it skips all multiples of 2, 3, and 5 using wheel factorization http://en.wikipedia.org/wiki/Wheel_factorization
//find largest prime factor for x <2^64
#include
#include
int main() {
uint64_t x = 600851475143;
int wheel[] = {4,2,4,2,4,6,2,6};
while(x>2 && x%2==0) x/=2;
while(x>3 && x%3==0) x/=3;
while(x>5 && x%5==0) x/=5;
for(uint64_t j=0, i=7; i<=x/i; i+=wheel[j++], j%=8) {
while(x>i && x%i==0) x/=i;
}
printf("%llu\n", x);
}
Another thing that could be done is to pre-compute all primes less than 2^32 (rather than downloading them) and then only divide by the primes. The fastest method I know to do this is the Sieve of Eratosthenes. Here is a version using OpenMP which finds the primes up to 1 billion in less than one second http://create.stephan-brumme.com/eratosthenes/