How to invoke the function itself

Asked

Viewed 75 times

-3

def R(n):

    if n == 1:
        return  1
    else:

        return  1/1/x-1/R(1/x+2/x)*n/1*n   
x=int(input("entre com x:"))

print(R(2))

she does the following handle 1 divides 1/x -1 each iteration it divides 1/ 1/x -1/3/x she adds 2/x acada iteration my doubt and how to call function her mistake Recursionerror: Maximum recursion Depth exceeded in comparison

  • had to mount recursive and iterative transform but I did code but I don’t know where to call function? ai bug all

  • What the function R should do ?

  • 1/1/x-1/3/x-1 if repair grows 2/x every iteration ai should calculate this

  • Your question is how to define the stop condition of the recursive call ?

  • Or how to invoke the function while executing it?

  • exactly that invoke function

  • You could better detail the question, using the [Edit] button, what exactly your function should do and what is currently happening?

  • You have an undeclared x variable within your function. I don’t understand that you are calculating. Variable x read out of function is not passed to function.

Show 3 more comments

1 answer

2

Just use the function name itself - in this case, R - and call it the same way you call it from outside.

for other algorithms that can be calculated with recursion, such as factorial, it is easier to understand:

def factorial(n):
   if n == 1: 
       return 1
   return n * factorial(n - 1)

With the notation used, and since I don’t know the function, I can’t understand what you want to calculate with your function to be able to rewrite it recursively.

The way it’s written there, you call R recursively with the value (1/x+2/x) but x and uam global variable defined outside the function - therefore except in a single special case, xwill never equal 1, and its function will never reach the stop value - the function will only terminate recursion, and start returning the values, when called with the value "1".

And even if you swap "x" for "n" in the call to R on the line return 1/1/x-1/R(1/x+2/x)*n/1*n, i don’t think the expression "1/x + 2 / x" will converge to "1" - this only happens with the value "3" in x - so I think you need to explain better the formula you want to calculate recursively if you want more help than this.

  • I wrote recursive but no wheel

  • increased the response with amis some considerations.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.