Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

somnamniac

  • 3 years ago

sanity check--what am I doing wrong copying the fibonacci function?

  • This Question is Closed
  1. somnamniac
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I think this is exactly what Professor Guttag uses in Lecture 13: def fib(n): global numCalls numCalls += 1 print 'fib called with ', n if n <= 1: return 1 else: return fib(n-1) + fib(n-2) numCalls = 0 n = 6 res = fib(n) print 'fib of ', n,' = ',res, 'numCalls = ',res but when I run it, it gives me fib(6) = 13 What am I missing?

  2. somnamniac
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Nevermind. 13 is the answer he got in the lecture as well. I was just printing res instead of numCalls and that got me confused. Although, according to http://en.wikipedia.org/wiki/Fibonacci_number, fib(6) actually IS 8, which is what he 'corrects' early in the lecture.

  3. ashwinram
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Fib(n) is the sum of the previous two numbers in the series. It's traditional to start with 0,1 which makes fib(6)=8 (the series is 0,1,1,2,3,5,8). But, you can also start with 1,1 (which is easier to code; if n<=1 return 1), which makes fib(6)=13 (the series is 1,1,2,3,5,8,13).

  4. somnamniac
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks!

  5. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.