1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Algorithm for fibonacci sequence

  1. Mar 19, 2014 #1
    1. The problem statement, all variables and given/known data
    create an algorithm for a Fibonacci sequence that will return the f value in f(n) = f(n-1)+f(n-2)

    2. Relevant equations

    f(n) = f(n-1)+f(n-2)

    3. The attempt at a solution

    I have tried several ways to create an algorithm that will sum the two previous numbers but always end up with the sequence 0,1,1,2,4,8,16 so the numbers end up double.

    please keep in mind I have only just started my intro to programming course and am just learning to write algorithms, I have no experience with programming languages at the present time.

    I've got something like this

    1 function fibonacci(a,b)
    2 if n=0,
    3 a<- 0
    4 b<- 1
    5 print a
    6 if n=1
    7 a<- (a+b) = (1+0) = 1
    8 print a
    9 if n=2
    10 b<- a = 1
    11 a<- (a+b) = (1+1) =2
    12 print a

    but as you can see, continuing on this path with only double the value each time. I need to find a way to sum numbers, depending on the value 'n' but cant seem to think of how to do this.

    any help is greatly appreciated.
  2. jcsd
  3. Mar 19, 2014 #2
    I see that you need to learn more about recursive functions. Here, this will help you out:
    https://www.khanacademy.org/science/computer-science-subject/computer-science/v/recursive-fibonacci-example [Broken]
    Last edited by a moderator: May 6, 2017
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted