sum recursive sequence

0

I'm implementing a recursive code that sums the following sequence: x + x ^ 2/2 + x ^ 3/3 ... x ^ n / n, for this sum, I thought of a definition, combining two recursive functions, like it goes down, but it is returning very high values, for n > 3.

def potencia(x, n):
    if n == 0: return 1
    else:
        return x * potencia(x, n - 1)

def Soma_Seq (x, n): 
    if n == 0: return 0
    else: 
        return x + Soma_Seq(potencia(x, n - 1), n - 1) / n
    
asked by anonymous 19.08.2017 / 17:59

1 answer

0

We can use this answer to your question:

Recursive function that simulates a high number to a power

def potencia(base, power, show_steps=True):
    """show_steps serve só para dizer 
    se amostrar os passos recursivos escritos ou não."""
    if power == 0: # caso base
        if show_steps:
            print(base, "^{0} = 1", sep="")        
        return 1
    else: # passo recursivo
        if show_steps:
            print(base, "^{", power, "} = ", base, " * ", base, "^{", power - 1, "}", sep="")
        return base * potencia(base, power - 1, show_steps)

def Soma_Seq (x, n): 
    if n == 0: return 0
    else: 
        return x + Soma_Seq(potencia(x, n - 1), n - 1) / n

I think it's a good starting point.

    
22.08.2017 / 16:22