-2
def fibonacci(n):
    first = 0
    second = 1
    count = 0
    while count <= n:
        if (second % 2 == 0):
            first, second = second, first + second
            count += 1
    return first, second

print (fibonacci(4000000))

Can somebody please explain what is wrong with this code? In IDLE the page restarts but returns no answer. By the way, I'm a beginner programmer, only just finished the CodeAcademy course.

Kor13
  • 17
  • 2
  • As a side-note, in Python you use `return first, second` to return 2 values. – ZdaR Jul 10 '15 at 17:12
  • Tip: put `print(first, second)` inside the `while` loop and observe what happens to the values. – Daniel Roseman Jul 10 '15 at 17:16
  • What does "even-number Fibonacci sequence" mean? Is it a list of all even Fibonacci numbers up to the given element? – TigerhawkT3 Jul 10 '15 at 17:18
  • 1
    Also, it looks like this might be [Problem 2 from Project Euler](https://projecteuler.net/problem=2). If it is, your algorithm and understanding of the problem are incorrect. – TigerhawkT3 Jul 10 '15 at 17:22
  • @TigerhawkT3 Yes it is! Please could you explain how so? – Kor13 Jul 10 '15 at 17:29
  • write out a few terms from the fibonacci sequence. Do you see a pattern? It's always odd, odd, even, odd, odd, even, isn't it? – NightShadeQueen Jul 10 '15 at 17:38
  • @NightShadeQueen Yeah I see the pattern now, but I'm assuming there's a alternate method, seems too much like a logical shortcut. Any suggestions? – Kor13 Jul 10 '15 at 17:50
  • 2
    It's project euler. You're supposed to take as many logical shortcuts as you can :P – NightShadeQueen Jul 10 '15 at 17:52

4 Answers4

0

second % 2 starts off as 1, which is not odd. Consequently your while loop doesn't run anything in the body, so the loop runs forever.

You probably want to always compute the next Fibonacci number, but only increment the count if the second is even.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
0

Your problem is that you have your count variable inside the if statement. You have created an infinite while loop. Move it outside of the if statement:

    if(second % 2 == 0):
        first, second = second, first + second
    count +=1

Also you will have to add more code to make this work properly.

jfish003
  • 1,232
  • 8
  • 12
0

Since this is for Problem 2 of Project Euler, you're computing the wrong values. It asks for the sum of all even Fibonacci numbers up to a value of four million, not the four millionth value. That would be too large.

Since we want to keep generating values, we'll use a generator and combine it with itertools.takewhile(). Then we'll filter() it down to the even values, and find the sum().

import itertools

def fibonacci_gen():
    first = 0
    second = 1
    while 1:
        first, second = second, first + second
        yield second

 

>>> a = fibonacci_gen()
>>> sum(filter(lambda y: not y%2, itertools.takewhile(lambda x: x<=4e6, a)))
4613732

For a solution that doesn't use these features:

def fibonacci_4m():
    result = 0
    first = 0
    second = 1
    while second <= 4e6:
        first, second = second, first + second
        if not second%2:
            result += second
    return result

 

>>> fibonacci_4m()
4613732
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • Thanks a lot you're right it is, however I only just completed the CodeAcademy course, so I'm not familiar with 'yield', 'itertools.takewhile', filter( ) and a few of the other syntax as of yet. Could you simplify the syntax please? – Kor13 Jul 10 '15 at 17:40
  • I highly recommend learning about them instead. You can find out about `yield` from [the relevant question in the "Related" sidebar](http://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python?rq=1), and `itertools.takewhile` and `filter` from the links embedded in my answer. – TigerhawkT3 Jul 10 '15 at 17:44
  • Having a tough time understanding the explanations on Stack Overflow, have you got any other simpler/ detailed explanations, you could refer me to? – Kor13 Jul 10 '15 at 22:34
  • @Kor13, I've added an alternate version that uses more basic operations. – TigerhawkT3 Jul 10 '15 at 22:49
0

In your while loop, nothing changes when the if statement fails to execute its conditional code. Try the following instead:

def main():
    for index in range(1, 41):
        print('even_fibonacci({}) = {}'.format(index, even_fibonacci(index)))

def even_fibonacci(index):
    for number in all_fibonacci():
        if not number & 1:
            index -= 1
            if not index:
                return number

def all_fibonacci():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

if __name__ == '__main__':
    main()
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117