range

When working with numbers, it’s sometimes very useful to work with sequences. In some applications, we might be interested in the sequence of numbers 0, 1, 2, ..., n. In another application, the important numbers might lie on a segment l, l + 1, l + 2, ..., r.
The range() command makes it easy to work with ranges of numbers. It can generate numbers in any range with any interval between each pair:
print(list(range(5)))         # [0, 1, 2, 3, 4]
print(list(range(7)))         # [0, 1, 2, 3, 4, 5, 6]
print(list(range(1, 5)))      # [1, 2, 3, 4]
print(list(range(4, 7)))      # [4, 5, 6]
print(list(range(5, 3)))      # []
print(list(range(2, 15, 4)))  # [2, 6, 10, 14]
print(list(range(11, 2, -1))) # [11, 10, 9, 8, 7, 6, 5, 4, 3]
range() can take 1, 2 or 3 arguments:
  • 1 argument r: generate numbers 0, 1, 2, ... r-1
  • 2 arguments l and r: generate numbers l, l+1, l+2, ..., r-1
  • 3 arguments l, r, and d: generate numbers l, l+d, l+2d, ..., r-1
Note that similar to slices of strings or lists, the range() also works with an inclusive start and an exclusive end.
When used with other expressions, we can use range() without the list(). In the examples above, we have used list() so that the print statement displays a nice output.

Challenge

Given an integer n as an input, write a program that would output the sum of numbers 1, 2, ... n.
Input
Output
2
3
5
15
 

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue