A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
hi, i understand how fibonacci number works but i don't understand the coding part of it. can someone please explain it to me?
anonymous
 5 years ago
hi, i understand how fibonacci number works but i don't understand the coding part of it. can someone please explain it to me?

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Do you mean you need help with how to build the function, or you're looking at the function on a handout and you need help figuring out what it does?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0i'm looking at the function but can't figure out what it does

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0This one? def fib(x): """Return fibonacci of x, where x is a nonnegative int""" if x == 0 or x == 1: return 1 else: return fib(x1) + fib(x2)

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0well, the first part is easy. the line: if x == 0 or x == 1: return 1 checks to see if x is 0 or 1 if x isn't 0 or 1, the next line returns fib(x1) + fib(x2) fib(x1) is going to go through the same process. If (x1) is greater than 1, THAT will also get sent to fib. But eventually fib(x1) will return an integer. When it does, the program will start to evaluate fib(x2) the same way. when fib(x2) returns an integer, the problem becomes simple addition.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0when you say "If (x1) is greater than 1, THAT will also get sent to fib" does that mean the function will go through fib(x1) then fib((x1)1 then fib(((x1)1)1) and so on also when will fib(x1) return an integer? i thought fib(x1) will always be an integer

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0when you say "If (x1) is greater than 1, THAT will also get sent to fib" does that mean the function will go through fib(x1) then fib((x1)1 then fib(((x1)1)1) and so on Exactly, UNTIL one of those (xan accumulating number of ones) terms is in the base case, that is, when the line that says if x == 0 or x == 1: return 1 is true, the function will stop calling fib(xwhatever) and will just return 1 also when will fib(x1) return an integer? i thought fib(x1) will always be an integer Instead of fib(x1) think of it as 'the integer that gets returned when fib(x1) is finished being evaluated' So it is an integer, but it takes some time (the time to evauate fib(x1)) to figutre out what the integer is.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0One way to see this is to consider x = 2. Since x is not 1, the function computes fib(1) + fib(0). But those two values are known to be 1, so the answer is 1+1 = 2. Now consider x = 3. Since x is not 1, the function computes fib(2) + fib(1). fib(2) expands to fib(1) + fib(0), so the answer is 1+1+1 = 3. And so on.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0thanks! i think i got so lets say x = 4 fib(4) = fib(3) + fib(2) = [fib(2) + fib(1)] + [fib(1) + fib(0)] =[fib(1) + fib(0) + fib(1)] + [fib(1) + fib(0)]
Ask your own question
Sign UpFind 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
 Engagement 19 Mad Hatter
 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.