MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / triangular_number

Function triangular_number

maths/special_numbers/triangular_numbers.py:11–37  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

9
10
11def 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
40if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected