Thursday, April 3, 2014

Problem 80 - Frink, and yay, another round number

Now I have solved problem 80, which means that I am done with the 70s and am moving on to another region. Problem 80 involves calculating square roots digit by digit. The method I found on wikipedia to do this requires doing arithmetic with very large integers (for 100 digits of the square root, you need a 100 digit integer). Luckily, doing stuff with big numbers is one of the reasons why Frink exists. And Frink made this problem fairly simple: my biggest issue with this problem was forgetting that 0 is indeed a legal digit in a square root...anyway, here is the solution, Frink is hard to time as it runs in java and pops up a gui, but including Frink startup time this runs in about 5 seconds, most of that being Frink startup time.

digisum = 0

for n = 2 to 100 
{
    if floor[sqrt[n]] * floor[sqrt[n]] < n 
    {
        p = 0
        c = n
        for i = 0 to 99 
        {
            x = 0
            do
            {
                x = x + 1
                y = x*(20*p + x)
            } while y <= c
            x = x - 1
            y = x*(20*p + x)
            digisum = digisum + x
            p = p*10 + x
            c = (c - y) * 100
        }
    }
}
println[digisum]

No comments:

Post a Comment