Summing Series on Python

Well keeping with the faith. I’ve been playing with python and trying to sum series of the form,

sum (1/x^n) for n=1 to infinity…. i.e. 1/x +1/x^2 + 1/x^3 + …. + 1/x^n
for n>1 and x>1

and I’ve come with a curious result,

1/x +1/x^2 + 1/x^3 + …. + 1/x^n = 1/(x-1)

which leads us to conclude that:

1/x – 1/(x-1) = 1/x^2 + 1/x^3 + …. + 1/x^n

not sure at all what that means but here’s a python program to calculate it:

# Programme to calculate simple power series results.

import math

def pseries(value, maxpower):

“”” sum (1/x^n) for n=1 to infinity…. i.e. 1/x +1/x^2 + 1/x^3 + …. + 1/x^n
for n>1 and x>1
value = x & maxpower =n “””

v=value
m=maxpower
series=range(m)
sum = 0

total=[math.pow(value, (x+1)) for x in series]

for x in total:

sum = (1.0/x) + sum

return total, sum

now I am not sure if that is the best way to do it, but it does the job, for example the results on pseries(3,10)

>>> pseries(3,10)
([3.0, 9.0, 27.0, 81.0, 243.0, 729.0, 2187.0, 6561.0, 19683.0, 59049.0], 0.49999153245609573)

total returns a list of all the powers of 3 up to 3^10, sum returns the sum of their reciprocals. i.e. the sum of the function. in this case approx 0.5, = (1/(3-1)) = 1/2

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s