[Python] Efficient calculation of Fibonacci series

Discussão em 'Python' iniciado por Stack, Dezembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    I'm working on a Project Euler problem: the one about the sum of the even Fibonacci numbers.

    My code:

    def Fibonacci(n):
    if n == 0:
    return 0
    elif n == 1:
    return 1
    else:
    return Fibonacci(n-1) + Fibonacci(n-2)

    list1 = [x for x in range(39)]
    list2 = [i for i in list1 if Fibonacci(i) % 2 == 0]


    The problem's solution can be easily found by printing sum(list2). However, it is taking a lot of time to come up with the list2 I'm guessing. Is there any way to make this faster? Or is it okay even this way...

    (the problem: By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.)

    Continue reading...

Compartilhe esta Página