All problems

Problem 230

Sum of First N Natural Numbers

Easy · 10 pts · Math

Problem

You are given a positive integer N. Print the sum 1 + 2 + 3 + ... + N. N can be up to one billion, so a loop would be slow. Use Gauss's formula instead: the sum equals N × (N + 1) / 2. Pairing the first and last numbers (1 + N), the second and second-to-last (2 + (N - 1)), and so on shows why: each of the N / 2 pairs adds up to N + 1. The result can reach about 5 × 10^17, so use a 64-bit integer.

Input

A single integer N.

Output

Print the sum.

Constraints

1 <= N <= 1000000000

Example input

5

Example output

15

Explanation

For N = 5: 1 + 2 + 3 + 4 + 5 = 15. The formula gives the same: 5 × 6 / 2 = 15.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Longest Happy Prefix Next: Digital Root