easy +10 pts

Pentagonal Number

Compute the nth pentagonal number using the closed-form formula.

Pentagonal numbers are figurate numbers that extend the concept of triangular and square numbers. The nth pentagonal number is given by the formula: P(n) = n(3n - 1) / 2 where n is a positive integer (1, 2, 3, ...). Your task is to implement a function `pentagonal(n)` that returns the nth pentagonal number as an integer. Input: A single positive integer `n` (1 ≤ n ≤ 10^6). Output: The nth pentagonal number as an integer. You can assume that the input is always valid based on the constraints.

Constraints

1 ≤ n ≤ 10^6 Time complexity: O(1) (using the formula directly). Space complexity: O(1).

Example

>>> pentagonal(1)
1
>>> pentagonal(2)
5
>>> pentagonal(3)
12
>>> pentagonal(4)
22
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use integer division (//) to ensure the result is an integer.
The formula is n * (3 * n - 1) // 2.
Double-check your formula for n=1 and n=2 to confirm the sequence 1, 5, 12, ...
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.