Answer this by using PYTHON 52B Triangular Recursion Problem
Answer this by using PYTHON
5-2-B Triangular Recursion
Problem 1-7-B involved triangular numbers. We did it before we learned about recursion. Re-do the solution using recursion.
Triangular numbers are numbers of objects that could be arranged in a triangle by making rows, with one more object in each row than in the previous row. Write a function that given a number, n, will formulaically compute the nth Triangular number. Write another function that displays the Triangular numbers up to and including n.
For you demo, use your formula function to calculate the 21st Triangular number. Paste the answer in the Online Text Area.
Solution
def computeN(n):
return n*(n+1)/2;
def display(n):
return n
def displayTriangle(n):
if(n == 1):
return \"\"
else:
return displayTriangle(n-1) + \"* \"
print \"n th Tringular number\ \"
resN = computeN(21)
for i in range(1,21):
print displayTriangle(i)
