-
-
Notifications
You must be signed in to change notification settings - Fork 49.7k
Add leonardo numbers algorithm #14028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
d4d94d4
1fca966
b33434c
273b09a
f9f22a7
1c462c1
0894113
cc19f27
df18c92
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| def leonardo_numbers(n: int) -> int: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: |
||
| """ | ||
| Return the n-th Leonardo number. | ||
| The 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 | ||
| >>> 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 == 0 or n == 1: | ||
| return 1 | ||
|
|
||
| a, b = 1, 1 | ||
| for _ in range(n - 1): | ||
| a, b = b, a + b + 1 | ||
|
|
||
| return b | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import doctest | ||
|
|
||
| doctest.testmod() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter:
n