Search code examples
pythonseriesfactorialexponential

Python program summation of a series


I tried to create a program in python which basically sums up a series up to the term which is less than 0 that goes like: 1+((2X)/1!)+((3X^2)/2!)+((4X^3)/3!).... I designed a basic term every term in the series that is ((A+1)((X)^A))/A! where A is incremented in a loop through the terms. The program below gives no compilation error however it is not showing the result. Please help. It would be really appreciated. Thank you!

import decimal
def fact(n):
    if n == 0:
        return 1
    else:
        return n*(fact(n-1))
x = int(input("Enter the value of x"))
A=0
summation=0
sumt1=(A+1)
sumt2=(x**A)
sumt3=fact(x)
sumt4=sumt1*sumt2
sumt5=sumt4/sumt3

while (sumt5 > 0.00) :
    summation = summation+sumt5
    A+=1
finalsum=decimal.Decimal(1+summation)
print("The value is")
print(finalsum)

Solution

  • Move your sumt variables into your while loop, and then break out of the loop when sumt5 <= 0.00:

    import decimal
    
    def fact(n):
        if n == 0:
            return 1
        else:
            return n*(fact(n-1))
    
    x = 1  # Hardcoded for this example, instead of prompting.
    A = 0
    summation = 1.0
    
    while True:
        sumt1 = A + 1
        sumt2 = x**A
        sumt3 = fact(A)  # Was `fact(x)` in original code, which didn't match your formula & prevented `sumt5 <= 0.00` from ever being true.
        sumt4 = sumt1 * sumt2
        sumt5 = sumt4/sumt3
    
        summation += sumt5
        A += 1
    
        if sumt5 <= 0.00:
            break
    
    
    finalsum=decimal.Decimal(summation)
    print("The value is {}.".format(finalsum))
    

    Updated Answer

    I wanted to understand what was happening better for myself, so I broke the summation out into a separate function and, rather than calculating all the individual terms separately, I plugged them directly into the function you gave in the OP: ((A+1)((X)^A))/A!. Here's my code:

    import decimal
    
    def factorial(n):
        if n == 0:
            return 1
        else:
            return n * factorial(n - 1)
    
    
    def summation(num):
        A = 0
        result = 1.0
    
        while True:
            term = (A + 1) * num**A / factorial(A)
            result += term
            A += 1
    
            if term <= 0.0:  # Break out of the loop when the `term <= 0`.
                break
    
        return result
    
    
    # Sample output.    
    for i in range(1, 11):
        final = decimal.Decimal(summation(i))
        print('summation({}) == {}'.format(i, final))
    

    When I ran it, I received the following output:

    final == 6.43656365691809018159119659685529768466949462890625
    final == 23.167168296791945891754949116148054599761962890625
    final == 81.3421476927506574838844244368374347686767578125
    final == 273.99075016572106733292457647621631622314453125
    final == 891.478954615459542765165679156780242919921875
    final == 2825.00155444914480540319345891475677490234375
    final == 8774.06526742766800452955067157745361328125
    final == 26829.62188337555198813788592815399169921875
    final == 81031.839275753838592208921909332275390625
    final == 242292.12374287386774085462093353271484375