Back to Practice Dashboard
Python BasicsEasy
Sum of First N Natural numbers
Learn how to solve the 'Sum of First N Natural numbers' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
Write a function sum_natural(n) that takes a positive integer n and returns the sum of the first n natural numbers, i.e., 1 + 2 + 3 + ... + n.
Constraints
- •1 <= n <= 10^6
Examples
Example 1
Input
n = 5
Output
15
Explanation
1 + 2 + 3 + 4 + 5 = 15.
Example 2
Input
n = 10
Output
55
Explanation
1 + 2 + 3 + ... + 10 = 55.
Example 3
Input
n = 1
Output
1
Explanation
The sum of the first 1 natural number is just 1.
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.