I need to calculate the sum of the sequence in Python but I can't use the built-in functions for exponentiation.
It means I can't use **
and pow()
. I have to create my own function for that.
So I created the function for exponentiation but it works only for numbers. I need to calculate my formula to the nth.
My function for exponentiation:
def exponentiation(a,b):
result = 1
for index in range(b):
result = result * a
return result
For numbers, it works. But when I want to do this to the nth (I defined 'n' as a Symbol) I get:
'Symbol' object cannot be interpreted as an integer
So I don't know how to fix that.
And if I want to calculate the sum of the sequence, I use and it works:
sy.summation((-1/2)**n, (n,1, oo))
But as I said before I need to change **
to my own exponentiation function, but it still shows that 'Symbol' object cannot be interpreted as an integer.
sy.summation(exponentiation((-1/2),n), (n,1, oo))
Do you have any advice?
The 'nth' means any given number. So you don't need to (and I can't think how you would) exponentiate any symbols. I think you can maybe simplify things a bit, if you return a list instead of just the nth value:
def exponentiation(a, n):
result = 1
exponents_list = []
for i in range(n):
result *= a
exponents_list.append(result)
return exponents_list
then work with the list with a for loop to get your sum
if you have to work with sympy, check this answer: Summation over a sympy Array