Friday, December 20, 2013

Problem 73 - Groovy

I haven't solved a problem in a while because of finals and break and such...and another reason is because I am playing with some very frustrating esolangs that have yet to bear any fruit. I decided to take a break from that and solve problem 73 in Groovy, a language which, like so many others, runs on the JVM and is very java-like.
def gcd(a,b) {
    if (b == 0) {
        return a
    } else {
        return gcd(b, a%b)
    }
}

def numFracs(int d) {
    int low  = d / 3 + 1
    int high = d / 2
    ret = 0
    for (i in low..high) {
        if (gcd(i,d) == 1) {
            ++ret
        }
    }
    return ret
}

ans = 0
for (i in 4..12000) {
    ans += numFracs(i)
}
println ans



No comments:

Post a Comment