sanity check--what am I doing wrong copying the fibonacci function?
MIT 6.00 Intro Computer Science (OCW)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
I think this is exactly what Professor Guttag uses in Lecture 13:
numCalls += 1
print 'fib called with ', n
if n <= 1:
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?
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.
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).