Python Forum
recursion - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: recursion (/thread-17190.html)



recursion - mcgrim - Apr-01-2019

How to write recursion on python?
I need to write something like
x sub (n)= x sub (n-1) + 10.
I am a bit lost since online is
quite impossible to find examples showing it.


RE: recursion - ichabod801 - Apr-01-2019

You just call a function from within itself. Of course, you also need a termination state to stop the recursion, which you did not specify. So here's an example with factorials:

def factorial(n):
    if n in (0, 1):
        return 1
    elif n < 0:
        raise ValueError('Factorial is not defined for negative numbers')
    else:
        return n * factorial(n - 1)



RE: recursion - mcgrim - Apr-01-2019

I'm sorry, but I don't find your explanation very clear.
What I would need is a recursive example similar (or the same)
to the one I wrote and translated into python code.


RE: recursion - ichabod801 - Apr-01-2019

(Apr-01-2019, 06:58 PM)mcgrim Wrote: What I would need is a recursive example similar (or the same)
to the one I wrote and translated into python code.

I can't do that. I explained why in my in my previous post. And we don't write code for people around here, we help people write their own code. So try it yourself, and if you have problems post your code here and I'll help you with it. Please post your code in Python tags, and clearly explain the problem you are having, including the full text of any errors.


RE: recursion - mcgrim - Apr-01-2019

this is what I came up with but not sure if is right.
For x sub (n)= x sub (n-1) + 10
would it perhaps be
def f(x):
    return x(i-1) + 10 
?


RE: recursion - ichabod801 - Apr-01-2019

You missed a bracket in your Python tags, I fixed it for you.

You are mixing things up a little. Your function name is f, that's what you want to call again. Your parameter is x, that's what you want to modify when you call f again.

def f(x):
    return f(x - 1) + 10
But you still have a problem here. This will keep calling f forever. Well, not quite. Eventually it will cause an error because you have recursed too deep. You need a termination state, a point where you stop calling f. In my factorial example, the termination state was 0 or 1, at which point I returned 1 rather than calling the factorial function again.


RE: recursion - BillMcEnaney - Apr-01-2019

(Apr-01-2019, 07:21 PM)mcgrim Wrote: this is what I came up with but not sure if is right.
For x sub (n)= x sub (n-1) + 10
would it perhaps be
def f(x):
    return x(i-1) + 10 
?
You need an if statement to make the computer decide when to stop recusing. Where does the value of i come from? Should i be another argument in your recursive function?

(Apr-01-2019, 11:42 PM)BillMcEnaney Wrote:
(Apr-01-2019, 07:21 PM)mcgrim Wrote: this is what I came up with but not sure if is right.
For x sub (n)= x sub (n-1) + 10
would it perhaps be
def f(x):
    return x(i-1) + 10 
?
You need an if statement to make the computer decide when to stop recusing. Where does the value of i come from? Should i be another argument in your recursive function?
I meant to type "recursing."