Generate the triangular number at the specified position. Args: position (int): The position of the triangular number to generate. Returns: int: The triangular number at the specified position. Raises: ValueError: If `position` is negative. Examples:
(position: int)
| 9 | |
| 10 | |
| 11 | def triangular_number(position: int) -> int: |
| 12 | """ |
| 13 | Generate the triangular number at the specified position. |
| 14 | |
| 15 | Args: |
| 16 | position (int): The position of the triangular number to generate. |
| 17 | |
| 18 | Returns: |
| 19 | int: The triangular number at the specified position. |
| 20 | |
| 21 | Raises: |
| 22 | ValueError: If `position` is negative. |
| 23 | |
| 24 | Examples: |
| 25 | >>> triangular_number(1) |
| 26 | 1 |
| 27 | >>> triangular_number(3) |
| 28 | 6 |
| 29 | >>> triangular_number(-1) |
| 30 | Traceback (most recent call last): |
| 31 | ... |
| 32 | ValueError: param `position` must be non-negative |
| 33 | """ |
| 34 | if position < 0: |
| 35 | raise ValueError("param `position` must be non-negative") |
| 36 | |
| 37 | return position * (position + 1) // 2 |
| 38 | |
| 39 | |
| 40 | if __name__ == "__main__": |
nothing calls this directly
no outgoing calls
no test coverage detected