The sum from 1 to n
Medium
Execution time limit is 1 second
Runtime memory usage limit is 128 megabytes
The sum of all integers from 1 to 100 can be calculated by tricky expedient. We divide all the numbers in pairs 1 and 100, 2 and 99, 3 and 98, etc. The sum of each pair is 101. The number of all pairs is 100 / 2 = 50. Therefore, the sum equals to (1 + 100) * 100 / 2. For an odd number of terms works the same formula, for example: 1 + 2 + 3 = (1 + 3) * 3 / 2 = 6.
Input
One integer n. Number n can be negative.
Output
Print the sum of all integers from 1 to n. It is guaranteed that the sum is 64-bit signed integer type.
Examples
Input #1
Answer #1
Input #2
Answer #2
Input #3
Answer #3
Note
If n = -3, then the sum equals to 1 + 0 - 1 - 2 - 3 = -5.
Submissions 15K
Acceptance rate 9%