Read the question, write Python on the right, then Run or Check.
QuestionHint and solution stay closed until you open them
Read n and print 1 + 2 + … + n. If n is 0, print 0.
You can loop, or use the closed-form Gauss formula n(n+1)/2. Try both and see they agree.
Input. One line: an integer n ≥ 0.
Output. One integer: the running total.
Constraints
- 0 ≤ n ≤ 1,000,000
Examples
Input
5
Output
15
Input
1
Output
1
Hint
- sum(range(1, n + 1)) is the direct way.
- The formula n * (n + 1) // 2 is O(1) — no loop needed. Use // so the result stays an integer.
Show correct code
Peek only after you have tried. You can still Check your own version.
n = int(input())
print(n * (n + 1) // 2)
main.pyPython · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.