# Sum of Integers from 1 to n

In this lesson, we will learn how to find the sum of numbers from 1 to n using recursion.

## What does the sum of integers from 1 to $n$ mean?

Natural numbers are all **positive numbers** starting with $1$. These can be written as follows:

$1,2,3,4,5,6,7,8,9,10......$

We want to write a program that takes a specific number and adds all the numbers from $1$ until that number.

For example, if $n = 5$, the sum of numbers from $1$ to $5$ is: $1 + 2 + 3 + 4 + 5 = 15$.

### Mathematical Notation

Let’s write it down mathematically:

$\sum_{i=1} ^{5} i$

$=$ $5$ $+$ $\sum_{i=1} ^{4} i$

$=$ $5$ $+$ $4$ $+$ $\sum_{i=1} ^{3}$

$=$ $5$ $+$ $4$ $+$ $3$ $+$ $\sum_{i=1} ^{2}$

.

.

.

$=5+4+3+2+1$

#### Generic Mathematical Notation

$\sum_{i=1} ^{n} i$

$=$ $n$ $+$ $\sum_{i=1} ^{n-1} i$

$=$ $n$ $+$ $(n-1)$ $+$ $\sum_{i=1} ^{n-2}$

.

.

.

$=$ $n$ $+$ $(n-1)$ $+(n-2)$ $ … +2+1$

## Implementation

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy