Back to Practice Dashboard
Competitive ProgrammingEasy
Sum from 1 to n
Learn how to solve the 'Sum from 1 to n' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
Write a function sum_1_to_n(n) that returns the sum of integers from 1 to n.
Constraints
- •1 <= n <= 10^5
Examples
Example 1
Input
sum_1_to_n(5)
Output
15
Explanation
1 + 2 + 3 + 4 + 5 = 15.
Example 2
Input
sum_1_to_n(10)
Output
55
Explanation
1 + 2 + ... + 10 = 55.
Need a Hint?
Define subproblem states, establish the recurrence relation, and use memoization (top-down) or tabulation (bottom-up).
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.