back to the lesson

## Sum all numbers till the given one

importance: 5

Write a function `sumTo(n)` that calculates the sum of numbers `1 + 2 + ... + n`.

For instance:

``````sumTo(1) = 1
sumTo(2) = 2 + 1 = 3
sumTo(3) = 3 + 2 + 1 = 6
sumTo(4) = 4 + 3 + 2 + 1 = 10
...
sumTo(100) = 100 + 99 + ... + 2 + 1 = 5050``````

Make 3 solution variants:

1. Using a for loop.
2. Using a recursion, cause `sumTo(n) = n + sumTo(n-1)` for `n > 1`.
3. Using the arithmetic progression formula.

An example of the result:

``````function sumTo(n) { /*... your code ... */ }

P.S. Which solution variant is the fastest? The slowest? Why?

P.P.S. Can we use recursion to count `sumTo(100000)`?

The solution using a loop:

``````function sumTo(n) {
let sum = 0;
for (let i = 1; i <= n; i++) {
sum += i;
}
return sum;
}

The solution using recursion:

``````function sumTo(n) {
if (n == 1) return 1;
return n + sumTo(n - 1);
}

The solution using the formula: `sumTo(n) = n*(n+1)/2`:

``````function sumTo(n) {
return n * (n + 1) / 2;
}

P.S. Naturally, the formula is the fastest solution. It uses only 3 operations for any number `n`. The math helps!