Jump to content
  • Advertisement
Sign in to follow this  
Zaris

python oddity

This topic is 4817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm running this function from the IDE if python. def fib(n): print ' n =', n if n > 1: return n * fib(n-1) else: print 'end of the line' return 1 it gives me this output for fib(5) n = 5 n = 4 n = 3 n = 2 n = 1 end of the line 120 why is it giving me the 120 at the end? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Well, you're calculating the factorial not the fibonnacci series. 5! is 120.

So if you're wondering why you're not getting the correct fib number that is why. If you want to know why the code works look at the factorial definition:

1*2*3*...*N= N!

Let's say you know the factorial of N-1, then all you need to do is multiply by N. How do you find N-1? Find N-2. And so on until you get to 1. If you wish to know how to calculate the fibonacci series then that's a whole other ball of wax.

EDIT: Okay, oops. I finally understand your question, I'm sorry. Maybe fib is printing the final return value?

Share this post


Link to post
Share on other sites
Most python IDE's will print the return-value of any function-call you make from the command-line, which is why you get the final '120' printed.

However, as nobodynews pointed out, your function calculates the factorial of n rather that the fibonacci number (which the name of the function seems to imply).

A function for calculating fib might be:

def fib(n):
if ( n == 1 ):
return 1;
if ( n == 2 ):
return 1;
return fib(n-2) + fib(n-1);


- Neophyte

Share this post


Link to post
Share on other sites
I think what he's asking is why it gets printed after the "End of the line". This is rather puzzling, indeed. I say a fork in your computer.

Share this post


Link to post
Share on other sites
fib(1) returns 1, but you aren't calling fib(1) from the IDE. The return value of fib(5) is 120, which is why the 120 is being printed.

Share this post


Link to post
Share on other sites
Then what is going on when I replace "return 1" with "return 0" and the result come out always zero; is the program returning the value * (the return value)

I know it strange to be concern with this detail, but I feel not knowing this information could lead to my programs not doing what I believe they should do.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zaris
Then what is going on when I replace "return 1" with "return 0" and the result come out always zero; is the program returning the value * (the return value)

I know it strange to be concern with this detail, but I feel not knowing this information could lead to my programs not doing what I believe they should do.


fib(which, actually (as others have said) is returning a factorial, not a fibonacci number) calls itself:

fib(5) = (5 * fib(4))
= (5 * (4 * fib(3)))
= (5 * (4 * (3 * fib(2))))
= (5 * (4 * (3 * (2 * fib(1)))))
= (5 * (4 * (3 * (2 * 1))))
= 120


So, if the return 1 was replaced with a return 0, the result would be (5 * (4 * (3 * (2 * 0)))), which is 0.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!