Thursday, August 7, 2014

Problem 94 - Yeti

I am moving in on the final stretch. I have a solution to problem 99 in reserve that I am waiting to post until I finish 98, so once I finish 98, there will be just one problem, number 100, left for me to solve in order to reach that big 3 digit goal. So, for problem 94, I dove again into the "fledgling language list" I found on google and found a language called Yeti. It claims to have functional syntax based on ML, but I think it has much more flexible syntax, and it runs on the JVM, which helped make the installation process nice and short (even if the executable didn't work, I could run the jar fine). This problem was a not-too-difficult transition over from C++, especially as the language has a few things that make it really good for scientific / Euler style computing (built-in sqrt function and high precision arithmetic - I am especially impressed with the high precision arithmetic, as this is built on the JVM, so it seems the developers did not take the easy way out and just use Java primitives for everything) and now I will get ready to use C++ on problem 98, which looks like it might be nontrivial, though also hopefully not too hard.

Solution runs in about 5s on my machine.


contribution x = (
    var c = 0;
    temp1 = 3 * x * x - (2 * x) - 1;
    root1 = int(sqrt(temp1));
    c := if root1 * root1 == temp1 then
        3 * x + 1 + c
    else
        c
    fi;
    temp2 = 3 * x * x + (2 * x) - 1;
    root2 = int(sqrt(temp2));
    c := if root2 * root2 == temp2 then
        3 * x - 1 + c
    else
        c
    fi;
    c;);

var ans = 0;
var i   = 3;
i * 3 < 1000000000 loop (
    ans := ans + contribution(i);
    i := i + 2;
);
println(ans);

     

No comments:

Post a Comment