Tag Archives: recurrence

Nth Pell Number (Code Wars 13)

I typically don’t like math problems. This one seems like a “small” enough one to maybe introduce students to and get familiar with the notation, so they have at least some tools if an interviewer decides to spring something like this on them. So I like it for that reason. Basically a variation of the fib. sequence.

The commitment to the “get” method is a bit odd. Maybe really trying to get to the memoization, stateful approach? Not really needed, unless if I’m really missing something…. Presumably a limitation of the test design itself.

class Pell(object):
    def get(self, n):
        if n == 0:
            return 0
        if n == 1:
            return 1
        nprev = 1
        x = 2
        for i in range(3,n+1):
            nprevprev = nprev
            nprev = x
            x = (2*nprev) + nprevprev
        return x