Friday, June 7, 2013

[Project Euler] Problem 3 in C

Problem: Project Euler Problem 3

Very simple program that uses factorization. No need to create sieve of Eratosthenes like I did in the following post. [Project Euler] Problem 3a

In my version, I originally used unsigned long long int but I saw from the answer forum that we can use __int64. Anyways, I knew from the number that it's not divisible by 2 so I started from 3 and then only checked for odd divisors so that I can halve the execution time. The best time was 0.007s but averaged around 0.011s.

#include 

int main()
{
    __int64 number = 600851475143, divisor = 3;
    while (number > 1) {
        if (!(number % divisor)) {
            number /= divisor;
            divisor -= 2;
        }
        divisor += 2;
    }
    printf("%d\n", divisor);

    return 0;
}

Answer:

No comments:

Post a Comment