Wednesday, January 29, 2014

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

No comments:

Post a Comment