Wednesday, January 29, 2014

Introducing melancholytree

It has indeed been quite a while since I posted an update to this blog, and thus I don't think that I have anywhere here mentioned that I have secured for myself a domain name, melancholytree.com. For now, it just has links to this blog and my other projects, but it may eventually be the future home of the entire Project Euler Language Challenge.

After a very long hiatus, problem 74 Genie

So, it has been a little while since I have solved a Project Euler problem for this challenge - I decided to take Winter Break off, and now that I have been at school, classes (and by classes I mean game jams) have been taking up most of my time. However, with some encouragement, I came to the conclusion that it was time to get back into the swing of things. So, today I used Genie, a language built on the same infrastructure as Vala (hmm...I wonder what language I will use next?), but with pythonic syntax. Due to the pythonic syntax, this was not all that bad. Only real issue is that lists did not seem to work as the documentation said they would, so I had to use a fixed-length array instead. Oh well, good thing the problem specifices the maximum length I need be concerned with. Solution runs in ~35s on my machine.
[indent=4]


def factChainLength(x : int) : int
    var chain = new array of int[60]
    chain[0] = x
    var cl = 1
    while cl <= 60
        x = digiFacSum(x)
        if x in chain
            return cl
        else
            chain[cl] = x
            cl++

    return cl

def digiFacSum(x : int) : int
    var ret = 0
    while x > 0
        ret += factorial(x%10)
        x   /= 10
    return ret

def factorial(x: int) : int
    if x < 2
        return 1
    else
        return x * factorial(x-1)
init
    var ans = 0
    for var i = 1 to 1000000
        if factChainLength(i) == 60
            ans += 1
    print "%d", ans