Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 44 Online
OpenStudy (anonymous):

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

OpenStudy (anonymous):

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?

OpenStudy (anonymous):

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.

OpenStudy (ashwinram):

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).

OpenStudy (anonymous):

Thanks!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
Breathless: womp
55 minutes ago 0 Replies 0 Medals
Breathless: yo who wanna match pfp?
57 minutes ago 11 Replies 1 Medal
Ylynnaa: This was long time ago lmk if u fw itud83dude1d
4 hours ago 17 Replies 2 Medals
abound: Wow question cove really fell off
6 hours ago 6 Replies 1 Medal
ayden09: chat i love black pink hehe i like jones to
6 hours ago 20 Replies 2 Medals
kamani7676: help
1 day ago 5 Replies 1 Medal
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!