easy +10 pts

Triangular Number

Compute the nth triangular number efficiently.

A triangular number T(n) counts the number of dots that can form an equilateral triangle with n dots on a side. It is defined as T(n) = 1 + 2 + ... + n. For example, T(1)=1, T(2)=3, T(3)=6. Write a function `triangular_number(n)` that takes a non-negative integer n and returns the nth triangular number. You must implement the function exactly with this signature.

Constraints

0 <= n <= 10^6 The expected time complexity is O(1). The result fits within Python's integer range.

Example

>>> triangular_number(1)
1
>>> triangular_number(2)
3
>>> triangular_number(3)
6
>>> triangular_number(10)
55
>>> triangular_number(0)
0
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Recall the formula for the sum of the first n natural numbers.
Use integer arithmetic; n*(n+1)//2 works for all non-negative integers.
The formula gives O(1) time, which is ideal for large n.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.