Source

Adrian Neumann, Simple Programming Problems, Elementary #4

Description

Sum a range of numbers

Given a number n, return the sum of the numbers from 1 to n, inclusive.

1
2
3 ⟹ 6
10 ⟹ 55

My solution

Using standard library sum() and range():

1
2
def sumto(n):
    return sum(range(1, n + 1))

Tests

1
2
3
4
5
from sumto import sumto

def test_sumto():
    assert sumto(3) == 6
    assert sumto(10) == 55