-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Update lucas_series.py to include another method #3620
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3bd6b8e
Update lucas_series.py
Kush1101 54d55ab
Fix pre-commit error
Kush1101 cc94f5c
Update lucas_series.py
Kush1101 b16fb55
Update lucas_series.py
Kush1101 d47b505
Update lucas_series.py
Kush1101 c5471bb
Update lucas_series.py
Kush1101 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,69 @@ | ||
# Lucas Sequence Using Recursion | ||
""" | ||
https://en.wikipedia.org/wiki/Lucas_number | ||
""" | ||
|
||
|
||
def recur_luc(n): | ||
def recursive_lucas_number(n): | ||
Kush1101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
>>> recur_luc(1) | ||
Returns the nth lucas number | ||
>>> recursive_lucas_number(1) | ||
1 | ||
>>> recur_luc(0) | ||
>>> recursive_lucas_number(20) | ||
15127 | ||
>>> recursive_lucas_number(0) | ||
2 | ||
>>> recursive_lucas_number(25) | ||
167761 | ||
>>> recursive_lucas_number(-1.5) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: recursive_lucas_number accepts only integer arguments. | ||
""" | ||
if n == 1: | ||
return n | ||
if n == 0: | ||
return 2 | ||
return recur_luc(n - 1) + recur_luc(n - 2) | ||
if not isinstance(n, int): | ||
raise TypeError("recursive_lucas_number accepts only integer arguments.") | ||
|
||
return recursive_lucas_number(n - 1) + recursive_lucas_number(n - 2) | ||
|
||
|
||
def dynamic_lucas_number(n: int) -> int: | ||
""" | ||
Returns the nth lucas number | ||
>>> dynamic_lucas_number(1) | ||
1 | ||
>>> dynamic_lucas_number(20) | ||
15127 | ||
>>> dynamic_lucas_number(0) | ||
2 | ||
>>> dynamic_lucas_number(25) | ||
167761 | ||
>>> dynamic_lucas_number(-1.5) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: dynamic_lucas_number accepts only integer arguments. | ||
""" | ||
if not isinstance(n, int): | ||
raise TypeError("dynamic_lucas_number accepts only integer arguments.") | ||
if n == 0: | ||
return 2 | ||
if n == 1: | ||
return 1 | ||
a, b = 2, 1 | ||
for i in range(n): | ||
a, b = b, a + b | ||
return a | ||
|
||
|
||
if __name__ == "__main__": | ||
limit = int(input("How many terms to include in Lucas series:")) | ||
print("Lucas series:") | ||
for i in range(limit): | ||
print(recur_luc(i)) | ||
from doctest import testmod | ||
|
||
testmod() | ||
n = int(input("Enter the number of terms in lucas series:\n").strip()) | ||
n = int(input("Enter the number of terms in lucas series:\n").strip()) | ||
print("Using recursive function to calculate lucas series:") | ||
print(" ".join(str(recursive_lucas_number(i)) for i in range(n))) | ||
print("\nUsing dynamic function to calculate lucas series:") | ||
print(" ".join(str(dynamic_lucas_number(i)) for i in range(n))) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.