Saturday, September 28, 2013

Problem 48 - Quickly done in C

I am supposed to be writing a paper for a class. I got about 3 sentences done of it, so it seemed like a good time to take a break and solve a project Euler problem. This problem was quite easy, and I used C for the sake of expediency and ease, but I would not be too surprised if I come back soon and redo this problem in a pretty crazy language - 64 bit integers, modulus, multiplication, and control structures are really all that is needed to solve this problem in the blink of an eye. (well, 16ms, which I imagine is less than the time it takes to blink an eye).
#include <stdio.h>

int main(int argc, char** argv) {
    const unsigned long long BIG  = 10000000000;
    unsigned long long ans       = 0;
    unsigned long long summand   = 0;
    unsigned long long i;
    unsigned long long j;
    for (i = 1; i <= 1000; ++i) {
        summand = i;
        for (j = 1; j < i; ++ j) {
            summand *= i;
            summand %= BIG;
        }
        ans += summand;
        ans %= BIG;
    }
    printf("%lld\n",ans);
}

No comments:

Post a Comment