Friday, May 11, 2012

Project Euler, Problem 9 solution

Problem 9

JavaScript (SpiderMonkey)

var limit = 500,
    product = 0,
    pow_M = 0, 
    pow_N = 0;

for(var n = 3; n < limit; ++n) {
    for(var m = 4; m < limit; ++m) {
        product = m * (m + n);
        if(product == limit) {
            pow_M = Math.pow(m, 2);
            pow_N = Math.pow(n, 2);
            print((pow_M - pow_N) * (2*(m*n)) * (pow_M + pow_N));
            n = limit;
            break;
        }
    }
}

time: 0.01s memory: 4984 kB

Congratulations, the answer you gave to problem 9 is correct.
You are the 99646th person to have solved this problem.
You have earned 1 new award:
Decathlete: Solve ten consecutive problems

No comments:

Post a Comment