Sunday, August 11, 2013

Problem 21 - Squirrel

For my first new problem since the hackathon (problem 21), I decided to try Squirrel - I saw it on the list of languages that real people used to solve Project Euler problems, and the name sounded cool, so I thought it was worth a try. Turns out, its syntax is basically half C-like, half Python-like, so it was not hard to use at all. Anyway, I solved the problem.


Now, before I start doing any more, I need to change the formatting of my progress table, it is already overflowing the bounds of this blog. My Solution:
function sumOfDivisors(x)
{
    local sum = 0;
    if (x == (sqrt(x).tointeger()*sqrt(x).tointeger())) {
        sum += sqrt(x).tointeger(); 
    }
    for (local i = 2; i < sqrt(x); i +=1) {
        if (x % i == 0) {
            sum += i + (x / i);
        }
    }
    //only proper divisors - we do not count the number itself
    return sum + 1;
} 

local sum = 0;
for (local i = 3; i < 10000; i +=1) {
    local j = sumOfDivisors(i);
    if (i != j && sumOfDivisors(j) == i) {
        sum += i;
        print(i + ", " + j + "\n");
    }
}
::print(sum.tostring() + "\n");

No comments:

Post a Comment