-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathleonardo_numbers.py
More file actions
51 lines (41 loc) · 1.09 KB
/
leonardo_numbers.py
File metadata and controls
51 lines (41 loc) · 1.09 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
51
"""
Leonardo numbers are a sequence of numbers given by the recurrence:
L(n) = L(n-1) + L(n-2) + 1
with initial values L(0) = 1 and L(1) = 1.
Reference: https://en.wikipedia.org/wiki/Leonardo_number
"""
def leonardo_numbers(n: int) -> int:
"""
Return the n-th Leonardo number.
>>> leonardo_numbers(0)
1
>>> leonardo_numbers(1)
1
>>> leonardo_numbers(2)
3
>>> leonardo_numbers(3)
5
>>> leonardo_numbers(4)
9
>>> leonardo_numbers(20)
21891
>>> leonardo_numbers(-1)
Traceback (most recent call last):
...
ValueError: n must be a non-negative integer
>>> leonardo_numbers(1.5)
Traceback (most recent call last):
...
ValueError: n must be a non-negative integer
"""
if not isinstance(n, int) or n < 0:
raise ValueError("n must be a non-negative integer")
if n in (0, 1):
return 1
previous, current = 1, 1
for _ in range(n - 1):
previous, current = current, previous + current + 1
return current
if __name__ == "__main__":
import doctest
doctest.testmod()