re: Project Euler #2 - Even Fibonacci numbers VIEW POST

FULL DISCUSSION
 

Recursive caching implementation

def solve(n):
    even_terms = []
    i = 2
    current = 0
    while current <= n:
        current = fib(i)
        even_terms.append(current)
        i += 3
    return sum(even_terms)


def fib(n, cache={1: 1, 2: 2}):
    if n not in cache:
        cache[n] = fib(n-1, cache) + fib(n-2, cache)
    return cache[n]
code of conduct - report abuse