Back to Practice Dashboard
Python BasicsEasy
Sum of N natural numbers
Learn how to solve the 'Sum of N natural numbers' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
Write a function sum_n_natural(n) that takes a non-negative integer n and returns the sum of the first n natural numbers using the formula n * (n + 1) / 2. If n is 0, return 0.
Constraints
- •0 <= n <= 10^6
Examples
Example 1
Input
n = 6
Output
21
Explanation
Using the formula: 6 * 7 / 2 = 21.
Example 2
Input
n = 0
Output
0
Explanation
There are no natural numbers to sum, so the result is 0.
Example 3
Input
n = 100
Output
5050
Explanation
100 * 101 / 2 = 5050.
Need a Hint?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.