Showing posts with label math. Show all posts
Showing posts with label math. Show all posts

Wednesday, May 16, 2012

Project Euler, problem 1 solution

Problem 1

Stupid brute force, not masterpiece, which is generally not suitable on really large numbers. But it is easy.

JavaScript (Spider Monkey)

var num = 1000, sum = 0, res = 0;
while(num--){
    if(!(num%3) || !(num%5))
        res += num;
}
print(res);

// time: 0.02s memory: 4984 kB

Tuesday, May 15, 2012

Project Euler, problem 13 solution

Problem 13

Let's use the power of math coprocessor ;)

JavaScript (Spider Monkey)

var numbers = [
    3.7107287533902102798797998220837590246510135740250,
    4.6376937677490009712648124896970078050417018260538,
    ............... etc
    5.3503534226472524250874054075591789781264330331690
];

var sum = 0;
for(var i = numbers.length - 1; i > -1; --i) {
    sum += numbers[i];
}
var result = sum * 10000000000;
print(result.toString().substring(0,10));

// time: 0.01s memory: 4984 kB

Congratulations, the answer you gave to problem 13 is correct.
You are the 67470th person to have solved this problem.

Project Euler, problem 28 solution

Problem 28

Somebody named... Euler wrote:
"First I noted that for an n by n grid, and n being odd, the number in the top right corner is n2.
A little mathematical analysis told me that the other corners are given by: n2-n+1, n2-2n+2, and n2-3n+3.
Adding these together gives the quadratic, 4n2-6n+6.
Then all I had to do was create a loop from 3 to 1001 in steps of 2 and find the running total
(starting from 1) of the quadratic."

JavaScript (Spider Monkey)

var s = 1;
for(var n = 3; n <= 1001; n += 2) {
    s += 4 * Math.pow(n,2) - 6 * n + 6;
}
print(s);
// time: 0.02s    memory: 4984 kB

Congratulations, the answer you gave to problem 28 is correct.
You are the 39019th person to have solved this problem.