0 Replies - 329 Views - Last Post: 18 December 2008 - 04:38 PM

#1 atik97   User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

A function to find the sum of any series

Posted 18 December 2008 - 04:38 PM

Description: This function can find out the sum of any series.
#A function to find the sum of any series

def sum_of_series():
    n=int(raw_input("Enter the number of terms:"))  #Number of terms in the series
    a=float(raw_input("Enter 1st term:"))   #1st term of the series
    b=float(raw_input("Enter 2nd term:"))   #2nd term of the series
    c=float(raw_input("Enter 3rd term:"))   #3rd term of the series
    d1=b-a                                  #Common difference
    d2=c-b                                  #Common difference
    r1=b/a                                  #Common ratio
    r2=c/b                                  #Common ratio

    if d1==d2:                     #Check if the series is arithmetic
        s=float((n/2.0)*(2*a+(n-1)*d1))  #Sum of the arithmetic series
        print "The sum of the series is",s
        
    elif r1==r2:                   #Check if the series is geometric
        s=float((a*(r1**n-1))/(r1-1))   #Sum of the geometric series
        print "The sum of the series is",s
        
    else:
        print "The series is neither arithmetic nor geometric"



Is This A Good Question/Topic? 0
  • +

Page 1 of 1