Give a recursive algorithm for finding the sum of first npositive integers. Show all of your work.
Fantastic news! We've Found the answer you've been seeking!
Question:
Give a recursive algorithm for finding the sum of first npositive integers. Show all of your work. Hint: Use the formulan(n + 1) / 2
Here is my work so far
Define sum(n) by
sum(1) = 1
sum(n + 1) = sum(n) + n, where n >1
Algorithm
Procedure sum (n: positiveinteger)
if (n = 1)
sum(n) = 1
else
n := n + sum(n – 1)
I got this answer from thetextbook but it does not use the formula above. Is there anotherway to do this with the formula?
Related Book For
Discrete Mathematics and Its Applications
ISBN: 978-0073383095
7th edition
Authors: Kenneth H. Rosen
Posted Date: