Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6950001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:02:12+00:00 2026-05-27T14:02:12+00:00

Say, for example, the iterative and recursive versions of the Fibonacci series. Do they

  • 0

Say, for example, the iterative and recursive versions of the Fibonacci series. Do they have the same time complexity?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:02:13+00:00Added an answer on May 27, 2026 at 2:02 pm

    The answer depends strongly on your implementation. For the example you gave there are several possible solutions and I would say that the naive way to implement a solution has better complexity when implemented iterative. Here are the two implementations:

    int iterative_fib(int n) {
       if (n <= 2) {
         return 1;
       }
       int a = 1, b = 1, c;
       for (int i = 0; i < n - 2; ++i) {
         c = a + b;
         b = a;
         a = c;
       }
       return a;
    }
    int recursive_fib(int n) {
      if (n <= 2) {
        return 1;
      }
      return recursive_fib(n - 1) + recursive_fib(n-2);
    }
    

    In both implementations I assumed a correct input i.e. n >= 1. The first code is much longer but its complexity is O(n) i.e. linear, while the second implementation is shorter but has exponential complexity O(fib(n)) = O(φ^n) (φ = (1+√5)/2) and thus is much slower.
    One can improve the recursive version by introducing memoization(i.e. remembering the return values of the function you have already computed). This is usually done by introducing an array where you store the values. Here is an example:

    int mem[1000]; // initialize this array with some invalid value. Usually 0 or -1 
                   // as memset can be used for that: memset(mem, -1, sizeof(mem));
    int mem_fib(int n) {
      if (n <= 2) {
        return mem[n] = 1;
      }
      if (mem[n-1] == -1) {
        solve(n-1);
      }
      if (mem[n-2] == -1) {
        solve(n-2);
      }
      return mem[n] = mem[n-1] + mem[n-2];
    }
    

    Here the complexity of the recursive algorithm is linear just like the iterative solution. The solution I introduced above is the top-down approach for dynamic programming solution of your problem. The bottom-up approach will lead to something very similar to the solution I introduced as iterative.
    There a lot of articles on dynamic programming including in wikipedia

    Depending on the problems I have met in my experience some are way harder to be solved with bottom-up approach(i.e. iterative solution), while others are hard to solve with top-down approach.
    However the theory states that each problem that has an iterative solution has a recursive with the same computational complexity (and vice versa).

    Hope this answer helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have components that I need to iterate through, for example From and
I have 2 websites, lets say - example.com and example1.com example.com has a database
Say for example I have the following styles: #HorizNav ul li a.active:link { background-color:
Say for example, I have the following two tables: TableA { _id } TableB
say for example i have 2 div in a div like this <div style=width:
Let say for example, I have a struct, and an array of the struct.
Scenario: Let's say for example, I have two functions. First function is concerned with
Let's say for example i have 2 collections Comments and Users.The comments include username
I have class say for example public class Item { int price; String name;
This is a contrived example, but lets say I have declared objects: CustomObj fooObj;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.