-4

As stated above attempting to work out which of the following two programs is most efficient for finding the nth Fibonacci number and why.

The Fibonacci numbers are numbers in the sequence: 1, 1, 2, 3, 5, 8... where each number after the first two is the sum of the preceding two numbers.

//// iteration ////

int main()
{
int n;
int t1 = 0;
int t2 = 1;
int i;
int new_term;
printf("Enter N :");
scanf("%d", &n);
if (n == 1)
    new_term = 0;
else if (n == 2)
    new_term = 1;
else
{
    for (i = 3; i <= n; i++)
    {
        new_term = t1 + t2;
        t1 = t2;
        t2 = new_term;
    }
}
printf("%dth term of the series is %d", n, new_term);
return 0;
}

//// induction ////

int fibo(int n);

int main() {
      int n;
    printf("Enter n : ");
    scanf("%d", &n);
    printf("The %dth term of Fibonacci sequence is %d", n, fibo(n));
    return 0;
}

int fibo(int n)
{
    if (n == 1) return 0;
    if (n == 2) return 1;
    return fibo(n - 1) + fibo(n - 2);
}
Pang
  • 9,564
  • 146
  • 81
  • 122
altituda
  • 1
  • 1
  • 5
    This seems like a homework problem, so I won’t answer it. But one is *vastly* more efficient than the other. – Eike Schulte Apr 03 '17 at 14:38
  • 1
    Iteration is probably slighly faster, for obvious reasons. Think and you'll find out why by yourself. – Jabberwocky Apr 03 '17 at 14:39
  • 1
    What happened when you timed them? What happened when you checked CPU use when you ran them? What happened when you checked memory use when you ran them? – ThingyWotsit Apr 03 '17 at 14:42
  • Your recursive solution isn't quite right f(1) is 1 – cleblanc Apr 03 '17 at 14:44
  • @MichaelWalz Iteration isn't just slightly faster, it's obscenely faster. The 50th term took me 35 seconds to compute with the recursive version. The iterative version did the billionth term in 0.6 seconds. – David Apr 03 '17 at 14:46
  • 1
    The question would be better if you said where you were stuck. As @ThingyWotsit suggests, even if you can't analyse the programs, it's relatively easy to run them for various values of n, so it's hard to guess what the problem is. – Paul Hankin Apr 03 '17 at 14:46
  • Welcome to Stack Overflow! I'm voting to close this question as off-topic because Stack Overflow is for finding solutions to coding problems. For advice on improving working code, please use [codereview.se] instead. Your programs would be suitable for the `comparative-review` tag over there. – Toby Speight Apr 03 '17 at 15:20

1 Answers1

1

This is an algorithmic question and must be seen as one.

The iterative version will find its results in O(n) steps.

For the recursive version, as you use no memoization, each term will be computed many times.

Suppose N(n) is the number of steps to produce the nth number. You need to compute fibo(n-1) and fib0(n-2) and sum them. So you need N(n-1) + N(n-2) + 1 steps. Neglecting the last sum, it comes that N(n) is in the same order of magnitude of fibo(n) which is known to be exponential.

Optimized recursive algorithms can compute fibo(n) in O(n) steps at the cost of storing numbers in a array (they are still less efficient than the iterative way), but your naive algorithm will have terrible performance.

Recursion generally allows for very simple algorithms with very poor performances.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252