Thread: Help for loops

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    Quote Originally Posted by xuftugulus View Post
    Well the problem statement has your answer. From the definition of the fibonacci number, you have: F(1) = 1, F(2) = 1, F(N) = F(N-1) + F(N-2).
    Therefore, you need two variables to track the two previous numbers. You need a variable to track the current number that has been calculated, and should that reach your 'm', the for loop ends.
    (I suppose you need to do it with a for loop as an assignment, as a while loop would be cleaner and easier to write for this problem)
    And at every step of the loop after F(k) has been calculated it's simple to proceed as F(K+1) = F(K) + F(K-1), which means that the variable that held F(K-2) needs to contain what the variable that held F(K-1) has.

    Or you could use a recursive function which maps the problem and the solution of calculating a fibonacci number more closely than any non-recursive algorithm does.
    so i have a question should i implement a if statement, just so that the first two numbers are left alone, and then put the equation in for the loop as the else statement?
    Last edited by dals2002; 03-13-2008 at 09:19 PM.

Popular pages Recent additions subscribe to a feed