Write a program in python to solve the following series.
Answers
SOLUTION:
The given problem is solved using language - Python.
limit = 10
numerator = 1
denominator = 1
sum = 0
for i in range(1,limit+1):
sum += numerator/denominator
numerator += (i + 1)
denominator *= (i + 1)
print(f'The sum of first {limit} terms of the series is {sum}')
Explanation:
- Line 1: Initialise limit = 10 as we have to display the sum of first ten terms of the series.
- Line 2-3: Initialise numerator and denominator. These two variable will be used while calculating the sum.
- Line 4: Initialise sum = 0. 'sum' variable will calculate the sum of the series.
- Line 5: Loop is created.
- Line 6: Sum variable is updated.
- Line 7-8: Numerator and denominator value is updated to get the next term.
- Line 9: Finally, the sum is displayed!
For More..
☛ Recursive implementation of the problem.
Nth term of the series is given as:
Where:
The recursive algorithm is as follows:
The Code:
from math import factorial as fact
f=lambda x:x*(x+1)//2
def calc(limit):
if limit: # base case
return f(limit)/fact(limit) + calc(limit-1)
return 0
sum=calc(10)
print(f'The sum of first 10 terms of the series is {sum}')
Base case is the condition that prevents the recursive function from calling itself forever.
Output:
The sum of first 10 terms of the series is 4.077420910493827
SOLUTION:
The given problem is solved using language - Python.
limit = 10
numerator = 1
denominator = 1
sum = 0
for i in range(1,limit+1):
sum += numerator/denominator
numerator += (i + 1)
denominator *= (i + 1)
print(f'The sum of first {limit} terms of the series is {sum}')
Explanation:
Line 1: Initialise limit = 10 as we have to display the sum of first ten terms of the series.
Line 2-3: Initialise numerator and denominator. These two variable will be used while calculating the sum.
Line 4: Initialise sum = 0. 'sum' variable will calculate the sum of the series.
Line 5: Loop is created.
Line 6: Sum variable is updated.
Line 7-8: Numerator and denominator value is updated to get the next term.
Line 9: Finally, the sum is displayed!
For More..
☛ Recursive implementation of the problem.
Nth term of the series is given as:
\rm\longrightarrow a_{n}=\dfrac{f(n)}{n!}⟶a
n
=
n!
f(n)
Where:
\rm\longrightarrow f(n)=1+2+3+...+n=\dfrac{n(n+1)}{2}⟶f(n)=1+2+3+...+n=
2
n(n+1)
The recursive algorithm is as follows:
\begin{gathered}\rm\longrightarrow sum(n)=\begin{cases}\rm a_{n}+sum(n-1), n > 0\\ \rm0, n=0\end{cases}\end{gathered}
⟶sum(n)={
a
n
+sum(n−1),n>0
0,n=0
The Code:
from math import factorial as fact
f=lambda x:x*(x+1)//2
def calc(limit):
if limit: # base case
return f(limit)/fact(limit) + calc(limit-1)
return 0
sum=calc(10)
print(f'The sum of first 10 terms of the series is {sum}')
Base case is the condition that prevents the recursive function from calling itself forever.
Output:
The sum of first 10 terms of the series is 4.077420910493827