Python BasicsEasy

Sum of First N Natural numbers

Detailed guide and Python implementation for the 'Sum of First N Natural numbers' problem.

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?
Consider using Basics-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.