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 8356573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:13:58+00:00 2026-06-09T10:13:58+00:00

For example, the code in textbook — solving Fibonacci problem by using recursion —

  • 0

For example, the code in textbook — solving Fibonacci problem by using recursion — is like this:

cache = {}

def fibo(n):
    if n in cache :
        return cache[n]
    elif n <=2:
        cache[n] = 1
    else:
        cache[n] = fibo(n-1) + fibo(n-2)
    return cache[n]

However, i am concerned every time doing function calls, costs are needed. Why didn’t the textbook use this code instead, to avoid unnecessary function call:

cache = {}

def fibo(n):
    if n <=2:
        cache[n] = 1
    else:
        # to avoid unnecessary function call
        if n-1 in cache:
            f1 = cache[n-1]
        else:
            f1 = fibo(n-1)
        if n-2 in cache:
            f2 = cache[n-2]
        else:
            f2 = fibo(n-2)

        cache[n] = f1 + f2

    return cache[n]

In this way, we could avoid unnecessary function call before actually calling it.

Anyway, my question is, why don’t the authors of the textbook write the code in the second way?

  • 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-06-09T10:13:59+00:00Added an answer on June 9, 2026 at 10:13 am

    What you’re describing is dynamic programming. You’re using an array to store the steps of recursion, ala memoization. For something like Fibonacci, where there is more than one recursive call per iteration, dynamic programming is indeed the preferred technique.

    As for why the textbook showed you the code it did, most likely because the authors wanted to just demonstrate recursion without going into too many concepts at once.

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

Sidebar

Related Questions

Example code so I can start explaining this problem: <div style=z-index:5>5</div> <div style=z-index:2>2</div> <div
Is this example code valid? #include<vector> using namespace std; int main() { vector<int> vec(10);
Example code: #include <cstdlib> #include <iostream> using namespace std; class A { public: A(int
I tried the example code in this book to draw the contours in the
Example code from a module: somevar = a def myfunc(somevar = None): # need
Example code: http://jsfiddle.net/slolife/PnmxM/ I am asking this even though there are a number of
Example code: # -*- coding: utf-8 -*- from functools import wraps class MyClass(object): def
I tried using the example code from my previous question . However I get
Example code: jQueryElement.append(jQueryOtherElement.remove(#some-selector)) Will this make a copy of my element and append it
Example code: element.addEventListener('click',function () { this.style.backgroundColor = '#cc0000' //after element was clicked once what

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.