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

  • Home
  • SEARCH
  • 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 36141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:20:48+00:00 2026-05-10T14:20:48+00:00

For example, Look at the code that calculates the n-th Fibonacci number: fib(int n)

  • 0

For example, Look at the code that calculates the n-th Fibonacci number:

fib(int n) {     if(n==0 || n==1)         return 1;     return fib(n-1) + fib(n-2); } 

The problem with this code is that it will generate stack overflow error for any number greater than 15 (in most computers).

Assume that we are calculating fib(10). In this process, say fib(5) is calculated a lot of times. Is there some way to store this in memory for fast retrieval and thereby increase the speed of recursion?

I am looking for a generic technique that can be used in almost all problems.

  • 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. 2026-05-10T14:20:48+00:00Added an answer on May 10, 2026 at 2:20 pm

    Yes your insight is correct. This is called dynamic programming. It is usually a common memory runtime trade-off.

    In the case of fibo, you don’t even need to cache everything :

    [edit] The author of the question seems to be looking for a general method to cache rather than a method to compute Fibonacci. Search wikipedia or look at the code of the other poster to get this answer. Those answers are linear in time and memory.

    **Here is a linear-time algorithm O(n), constant in memory **

    in OCaml:  let rec fibo n =      let rec aux = fun         | 0 -> (1,1)         | n -> let (cur, prec) = aux (n-1) in (cur+prec, cur)     let (cur,prec) = aux n in prec;;    in C++:  int fibo(int n) {     if (n == 0 ) return 1;     if (n == 1 ) return 1;     int p = fibo(0);     int c = fibo(1);     int buff = 0;     for (int i=1; i < n; ++i) {       buff = c;       c = p+c;       p = buff;     };     return c; }; 

    This perform in linear time. But log is actually possible !!! Roo’s program is linear too, but way slower, and use memory.

    Here is the log algorithm O(log(n))

    Now for the log-time algorithm (way way way faster), here is a method : If you know u(n), u(n-1), computing u(n+1), u(n) can be done by applying a matrix:

    | u(n+1) |  = | 1 1 | | u(n)   | | u(n)   |    | 1 0 | | u(n-1) |     

    So that you have :

    | u(n)    |  = | 1 1 |^(n-1) | u(1) | = | 1 1 |^(n-1) | 1 | | u(n-1)  |    | 1 0 |       | u(0) |   | 1 0 |       | 1 | 

    Computing the exponential of the matrix has a logarithmic complexity. Just implement recursively the idea :

    M^(0)    = Id M^(2p+1) = (M^2p) * M M^(2p)   = (M^p) * (M^p)  // of course don't compute M^p twice here. 

    You can also just diagonalize it (not to difficult), you will find the gold number and its conjugate in its eigenvalue, and the result will give you an EXACT mathematical formula for u(n). It contains powers of those eigenvalues, so that the complexity will still be logarithmic.

    Fibo is often taken as an example to illustrate Dynamic Programming, but as you see, it is not really pertinent.

    @John: I don’t think it has anything to do with do with hash.

    @John2: A map is a bit general don’t you think? For Fibonacci case, all the keys are contiguous so that a vector is appropriate, once again there are much faster ways to compute fibo sequence, see my code sample over there.

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

Sidebar

Related Questions

I have a problem with internet explorer 6 and 7, please look this example
Example: http://bit.ly/dfjvmT If you take a look at that URL, you will see an
Take a look at my code example at js bin: http://jsbin.com/iritep/3/edit I'd like to
Take a look at the example code... NodeList folderNodes = document.getElementsByTagName(Folder); DocumentFragment node =
Take a look at this example: http://www.extjs.com/deploy/dev/examples/multiselect/multiselect-demo.html On it, there are components rendered against
Have a look a this simple example. I don't quite understand why o1 prints
Please take a look at this example: #include <iostream> #include <vector> #include <string> using
I have a couple of images, representing tents, that look like this: The red
I have a youtube video embed code that looks like this: <iframe width=560 height=315
I have come across a few Perl modules that for example look similar to

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.