ezoic

Tuesday, February 14, 2017

A python problem and its solution.


Write a program to calculate and display the sum of the series:
 
1 - 1/2 + 1/3 - 1/4 + ...
 
until a term is reached that is less than 0.0001.

Here is the program:

def aa(n):
   a=0
   if n>0:
    for i in range(1,n,1):
        a=a+(-1)**(i-1)*(1/i)
   return(a)

e=0.01

n=1
while e>0.0001:

    e=abs(aa(n+1)-aa(n))
    n=n+1
    if e<0.0001:
        print(e,aa(n),n)

And it converged. Results:



 


No comments:

Post a Comment

looking for a man

 I am a mid aged woman. I live in southern california.  I was born in 1980. I do not have any kid. no compliacted dating.  I am looking for ...