1

I'm supposed to change this recursive function, into an iterative function...

int rFib(int n) 
{   //assumes n >= 0
    if(n <= 1)
        return n;
    else
        return (rFib(n-1) + rFib(n-2));
}

But I'm drawing a blank on the mathematical view of this... I would appreciate any assistance. I was able to get the other 3 functions, but I just can't seem to figure out the math of this one.

public static int fib(int n)
{
    int theFib = 1;
    while(n > 1)
    {
        theFib = n - 1;
        n = n + n - 2;         
}
        System.out.println(theFib);
        return theFib;
    }
4
  • have a look at javacodegeeks.com/2014/02/dynamic-programming-introduction.html based on dynamic programming Commented Nov 4, 2015 at 2:15
  • 1
    'supposed to change this function'? Is this a homework question? its ok if it is, just be up front about it Commented Nov 4, 2015 at 2:16
  • 2
    for problems like these, i find it really helps to ignore the code completely at first, figure it out on paper or a whiteboard first. then after you know how it works, how would the code look? ie, you have two numbers, you add them together to get a third number, then use that number added to one of your first numbers to get a fourth number. then what happens? when do you stop adding together? Commented Nov 4, 2015 at 2:21
  • thanks for all the assistance you guys. and yeah, this is hw...I didn't think of having to save the numbers. much appreciated. Commented Nov 4, 2015 at 19:04

3 Answers 3

1

The next number in the Fibonacci sequence is the sum of the last two numbers, so you'll need to remember the last two numbers.

In pseudo code, since you should do some of the homework yourself:

n1 = 0
n2 = 1
loop
    n = n1 + n2
    n1 = n2
    n2 = n
end loop

I'll leave it to you to limit the looping.

Sign up to request clarification or add additional context in comments.

Comments

0

You can find an example here.

The code in question:

public class FibonacciIterative {
    public static int fib(int n) {
                int prev1=0, prev2=1;
                for(int i=0; i<n; i++) {
                    int savePrev1 = prev1;
                    prev1 = prev2;
                    prev2 = savePrev1 + prev2;
                }
                return prev1;
    }
}

It does not really matter which direction (up or down) you count. The challenge is to deal with the limits properly.

Comments

0

Using dynamic programming technique:

    static int fib(int n) {
        int[] fibs = new int[n + 1];

        for (int i = 0; i <= n; i++) {
            if (i <= 1) {
                fibs[i] = i;
            } else {
                fibs[i] = fibs[i - 1] + fibs[i - 2];
            }
        }
        return fibs[n];
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.