Login Register


Python Recursivity (Fibonacci) filter_list
Author
Message
Python Recursivity (Fibonacci) #1
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 Smile

Best regards.

AdrexX

Reply

RE: Python Recursivity (Fibonacci) #2
You can also do this:
Code:
a, b = 0, 1 while b < 50 #Change Limit here: print(b) a, b = b, a + b
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply

RE: Python Recursivity (Fibonacci) #3
Really adf.ly?? i mean you are asking for our ideas and want us to go through adf.ly.

Reply

RE: Python Recursivity (Fibonacci) #4
Thank you for the idea with the loop but I was hoping to still keep it in a recursive function Smile

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.

Reply

RE: Python Recursivity (Fibonacci) #5
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.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Python Recursivity (Fibonacci) #6
Link should be fixed, I will dig into the explaination, thank you for the response.

Reply

RE: Python Recursivity (Fibonacci) #7
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)
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply







Users browsing this thread: