Series using recursiveness

Asked

Viewed 88 times

1

I’m trying to make a show x^(n)/n! but although it seems simple I have a constraint that is: It has to be a recursive function. What I’ve got so far that I don’t even know if it’s right is this::

def serie(x,n):
    if (x and n) < 0:
        return 0
    elif n = 0:
        return 1
    else:

Basically missing the main one that I don’t really understand how I’m gonna do it.

Basically my idea would be to multiply the x by himself n sometimes using maybe a for or a range(although I don’t know if I can use the range command when I’m working with recursion) and then divide by n factorial I was thinking about doing n*n-1*n-2 ....

Until I reach 0 but in this part I have no idea how to do it.

1 answer

1


You can re-write the operation serie(x, n) = x^n / n! as

x^n   x * x^(n-1)    x    x^(n-1)    x
--- = ----------- = --- * ------- = --- * serie(x, n - 1)
 n!   n * (n-1)!     n     (n-1)!    n

For values of n >= 1. The last expression shows the recursive step of its function.

Browser other questions tagged

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