Topic: Recursion
Using recursion, write a program that returns the sum of the following series.
[tex]\tt 0+2+18+72+... + nth \: term[/tex]
_
#Challenge
Share
Topic: Recursion
Using recursion, write a program that returns the sum of the following series.
[tex]\tt 0+2+18+72+... + nth \: term[/tex]
_
#Challenge
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Verified answer
Hint:
For this series:
[tex]\rm\longrightarrow n^{th}\ term=\dfrac{[n(n-1)]^{2}}{2}[/tex]
The Code:
The problem is solved using Python.
f=lambda x: (x*(x-1))**2//2 # defines f(x)
sum=lambda n:f(n)+sum(n-1) if n else 0 # defines sum(n)
n=int(input('Enter limit: ')) # accepts limit.
print(f'Sum of first {n} terms of the series is {sum(n)}.') # displays sum.
Algorithm:
The sum can be calculated using the following recursive algorithm.
[tex]\rm\longrightarrow sum(n) = \begin{cases}\rm f(n)+sum(n-1), \ \ n>0\\ \rm0,\ \ n=0\end{cases}[/tex]
Where f(n) denotes the nᵗʰ term of the series, given by:
[tex]\rm\longrightarrow f(n)=\dfrac{[n(n-1)]^{2}}{2}[/tex]
See attachment for output.