![]() |
|
Python Recursivity (Fibonacci) - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Python (https://sinister.ly/Forum-Python) +--- Thread: Python Recursivity (Fibonacci) (/Thread-Python-Recursivity-Fibonacci) |
Python Recursivity (Fibonacci) - AdrexX - 03-13-2013 So I have made this recursive function which takes a user-input, and then multiply the input with all positive natural integers below the input (Also know as the Fibonacci sequence). In short, the user-input determines the max range for the Fibonacci sequence and calculates the product, what do you think? could it be written more simple? - Let me hear your ideas! Spoiler:Code: string = ''
def Fibonacci(n):
global string
if n == 1:
string += '1'
return 1
else:
string += str(n) + ' * '
return n*Fibonacci(n-1)
while 1:
print str(Fibonacci(input('Enter a number: '))) + ' = ' + string
raw_input('Press enter to type again...')
print ''
string = ''I was also thinking about doing some tutorials in python, starting of easily with arithmetic operators and then continue to more advanced stuff later on, like a long tutorial with examples in every little part. Hopefully this could help some beginners to get even better, let me hear what you think! I will not waste any time if the community does not need it, so leave a comment ![]() Best regards. AdrexX RE: Python Recursivity (Fibonacci) - Ex094 - 03-14-2013 You can also do this: Code: a, b = 0, 1
while b < 50 #Change Limit here:
print(b)
a, b = b, a + bRE: Python Recursivity (Fibonacci) - H4R0015K - 03-14-2013 Really adf.ly?? i mean you are asking for our ideas and want us to go through adf.ly. RE: Python Recursivity (Fibonacci) - AdrexX - 03-14-2013 Thank you for the idea with the loop but I was hoping to still keep it in a recursive function ![]() Response: H4R0015K I see your point and I actually feel terribly sorry! In the beginning it was not mean to be a question thread in this manner of speaking, therefore I felt free to put it in a adf.ly link but I get your point, it is wrong to ask You something and then make you go through commercials, respect-less indeed. I have removed the adf.ly redirect and made the link direct, I hope you didn't prejudged me too hard, I really don't wanna make this cheap impression of myself, it is just embarrassing. Sincerly AdrexX. RE: Python Recursivity (Fibonacci) - Deque - 03-14-2013 At the moment there is no link at all. I suggest you just post the code in code-tags here. It wasn't that much. I looked into it, but the only thing I can remember by now, was that you aren't able to exit the program. It says press enter if you want to continue, but you can't do anything to make it stop (besides just killing it). A recursive fib is already very simple, but may have problems with larger fib-numbers where an imperative solution still works like a charm. I think you can improve your presentation by explaining the fib series, recursion, the reason recursion is used at all and your code a bit more. RE: Python Recursivity (Fibonacci) - AdrexX - 03-14-2013 Link should be fixed, I will dig into the explaination, thank you for the response. RE: Python Recursivity (Fibonacci) - ArkPhaze - 03-19-2013 Your function is flawed too, you don't account for 0. Fib(0) is still 1. Code: def Fib(x):
"""
returns Fib of x. (assumes x > -1)
"""
if x == 0 or x == 1:
return 1
else:
return fib(x-1) + fib(x-2) |