-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathpadovan_sequence.py
More file actions
50 lines (40 loc) · 1.2 KB
/
padovan_sequence.py
File metadata and controls
50 lines (40 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def padovan_sequence(n: int) -> int:
"""
Return the n-th term of the Padovan Sequence.
The Padovan sequence is the sequence of integers P(n) defined by the initial values
P(0) = P(1) = P(2) = 1 and the recurrence relation P(n) = P(n-2) + P(n-3).
https://en.wikipedia.org/wiki/Padovan_sequence
:param n: The index of the term to return.
:return: The n-th term of the Padovan Sequence.
>>> padovan_sequence(0)
1
>>> padovan_sequence(1)
1
>>> padovan_sequence(2)
1
>>> padovan_sequence(3)
2
>>> padovan_sequence(4)
2
>>> padovan_sequence(5)
3
>>> padovan_sequence(10)
12
>>> padovan_sequence(-1)
Traceback (most recent call last):
...
ValueError: Input must be a non-negative integer.
"""
if not isinstance(n, int) or n < 0:
raise ValueError("Input must be a non-negative integer.")
if n <= 2:
return 1
p_prev_3, p_prev_2, p_prev_1 = 1, 1, 1
for _ in range(3, n + 1):
curr = p_prev_2 + p_prev_3
p_prev_3, p_prev_2, p_prev_1 = p_prev_2, p_prev_1, curr
return p_prev_1
if __name__ == "__main__":
import doctest
doctest.testmod()
print("Doctests passed!")