Thursday, November 14, 2013

Problem 39 - Redo in Gosu

Gosu is a fairly fun language based that runs on the JVM. As it runs on the JVM, it has access to Java classes and stuff, so it has access to collections like maps, and all that fun stuff. As such, I decided to use Gosu in order to resolve problem 39, which I had previously solved in python by leveraging the Python dicts in order to make things simple: in Gosu, I just used Java HashMaps (though with a bit less of the clunky Java syntax). The hardest part of this solution was finding how to use Math.sqrt in gosu...oh well. Solution runs in about 2.5 seconds on my machine.
uses java.lang.*
    
var sols = { 0 -> 0 }

for (a in 2..500) {
    for (b in a..500) {
        var c2 = a*a + b*b
        var c  = Math.sqrt(c2) as int
        if (c2 == c * c) {
            var p = a + b + c
            if (p <= 1000) {
                if (!sols.containsKey(p)) {
                    sols.put( p, 1)
                } else {
                    sols.put( p, sols.get(p) + 1)
                }
            }
        }
    }
}

var ans = 0
var maxsols = 0
for (p in sols.keySet()) {
    if (maxsols < sols.get(p)) {
        ans = p
        maxsols = sols.get(p)
    }
}
print(ans)
     

No comments:

Post a Comment