Recursion: Sum

Published by Werdna in

Write a function that finds the sum of the first $n natural numbers. Make your function recursive.

Examples

sum(5) ➞ 15
// 1 + 2 + 3 + 4 + 5 = 15

sum(1) ➞ 1

sum(12) ➞ 78

Notes

  • Assume the input number is always positive.
  • Check the Resources tab for info on recursion.
Watch a quick demo on how Edabit works.