Jump to content

Beginner problem: Fibonacci sequence

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
TheGame

TheGame

    Newbie

  • Members
  • Pip
  • 2 posts
Because this is my first post, I would like to say Hello to all of you.

I'm new to python and run into a problem. I tried to resolve the problem with functions, but without success. Maybe somebody can tell me what I did wrong.
This is the problem. I resolved it without functions and it works.

#!/usr/bin/python3


########################################################################################################################################################

###                                                                                                                                                  ###

###   Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:   ###

###                                                                                                                                                  ###

###   1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...                                                                                                         ###

###                                                                                                                                                  ###

###   Find the sum of all the even-valued terms in the sequence which do not exceed four million.                                                    ###

###                                                                                                                                                  ###

########################################################################################################################################################


n = int(input('Give n:'))


fib1, fib2 = 1, 1

fibo = fib1 + fib2

s=0


while fibo <= n:

  if fibo % 2 == 0:

    s += fibo

  fib1 = fib2

  fib2 = fibo

  fibo = fib1 + fib2


print('Sum is:', s)

print('--- Done ---')


Now this is what I tried to do. A function for the Fibonacci sequence, but it doesn't work. It seems to do an infinite loop in the terminal. What did I do wrong?


#!/usr/bin/python3


n = int(input('Give n:'))

s = 0


fib1, fib2 = 1, 1

fibo = fib1 + fib2


def fibonacci(fib1, fib2):

  fibo = fib1 + fib2

  yield fibo

  fib1 = fib2

  fib2 = fibo


while fibo <= n:

  if fibo % 2 == 0:

    s += fibo

  fibonacci(fib1, fib2)


print('Sum is:', s)

print('--- Done ---')


Sorry for any writing mistakes, my English isn't that good.

#2
Nyuu

Nyuu

    Newbie

  • Members
  • Pip
  • 2 posts
Idk if I understand your problem. Are you just trying to get the Fibonacci sequence into a function? Because all you'd have to do is:

def Fibonacci():

    x1, x2 = 1, 2

    fibo = x1 + x2

    print x1

    print x2

    print fibo

    

    while fibo < 4000000:

        x1 = x2

        x2 = fibo

        fibo = x1 + x2

        print fibo

And then when you call Fibonacci(), it'll run it. My program is a little different than yours. It doesn't ask for the input, it just goes to 4,000,000, but you should easily be able to make it ask for input instead. I'm a beginner as well, so I may be wrong about what you're trying to accomplish, but yea.


Edit: I'll go ahead and add that your problem may be using input() to get n. Honestly I don't know the differences between raw_input and input, but I've only heard bad things about using input, so yea. But I just tested:

n = int(raw_input("Enter a number to stop the sequence at: "))

x1, x2 = 1, 2

fibo = x1 + x2

print x1

print x2

print fibo

    

while fibo < n:

    x1 = x2

    x2 = fibo

    fibo = x1 + x2

    print fibo


raw_input("Press the Enter key to exit")

in a function and it worked fine. So again, I may just be misunderstanding your question. Sorry if this hasn't helped.

#3
Sky

Sky

    Learning Programmer

  • Members
  • PipPipPip
  • 83 posts
Sorry to tell you, but Fibonacci Sequence starts with 1, 1 not 1, 2.

http://www.mathacademy.com/pr/prime/articles/fibonac/index.asp


#4
Nyuu

Nyuu

    Newbie

  • Members
  • Pip
  • 2 posts
Shouldn't matter. Change x1, x2 to 1, 1.

#5
TheGame

TheGame

    Newbie

  • Members
  • Pip
  • 2 posts
Nyuu Thank you for trying to help me. This is what I want to do: make a function called Fibonacii, that calculates the next fibonacci term, and returns that term so i could use it later in the problem. The problem is, that my way generates an infinite loop and I don't know why.
while fibo <= n:
  if fibo % 2 == 0:
    s += fibo
  fibonacci(fib1, fib2)

I think that the problem is here, when I call the fibonacci function. fib1 and fib2 should be global? Anyway, 10x for the help.