Sum of First N Natural Numbers
Given a non-negative integer n, return the sum of all natural numbers from 1 through n. When n is 0, the range is empty and the answer is 0.
Given a non-negative integer `n`, return the sum of all natural numbers from `1` through `n`. When `n` is `0`, the range is empty and the answer is `0`.
- `0 <= n <= 10000` - The answer fits within a 32-bit signed integer.
Example 1: input n = 5; output 15. 1 + 2 + 3 + 4 + 5 = 15.
Example 2: input n = 0; output 0. There are no positive integers to add.
Example 3: input n = 10; output 55. The integers from 1 through 10 sum to 55.
Category: Math. Topic: math. Difficulty: Easy. Patterns: math.