Solution Review: Compute Sum of First n Natural Numbers

This lesson will explain how to compute the sum of the first n numbers using recursion.

We'll cover the following

Solution: Use Recursion

The sum is calculated by adding the sum of previous numbers down to 1.

f(1) = 1 # base case
f(n) = f(n-1) + n # recursive case

The recursion stops when n is less than or equal to 1. The following illustration explains how to calculate the sum of the first n natural numbers using recursion.

Get hands-on with 1000+ tech skills courses.