easy +10 pts

Nth Triangular Number

Compute the nth triangular number from its closed-form formula.

A triangular number is the sum of the first n positive integers. For example, the 4th triangular number is 1 + 2 + 3 + 4 = 10. Write a function `triangular(n: int) -> int` that returns the nth triangular number. You may assume n is a non-negative integer. The function must handle large n efficiently using a direct formula rather than a loop.

Constraints

0 ≤ n ≤ 10^6. The result fits within a 64-bit integer. The function must run in O(1) time and O(1) space.

Example

>>> triangular(1)
1
>>> triangular(2)
3
>>> triangular(4)
10
>>> triangular(10)
55
>>> triangular(0)
0
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use the formula n * (n + 1) // 2.
Integer division is important to get an exact integer result.
Test with n=0, n=1, and a large n to verify performance.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.