-
-
Notifications
You must be signed in to change notification settings - Fork 51.1k
Add Jacobsthal numbers to maths/special_numbers #15409
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
cclauss
merged 2 commits into
TheAlgorithms:master
from
gowthaambr:add-jacobsthal-numbers
Sep 23, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| """ | ||
| Jacobsthal numbers | ||
|
|
||
| The Jacobsthal sequence starts with 0 and 1, and every later term is the | ||
| previous term plus twice the term before it: | ||
|
|
||
| J(0) = 0 | ||
| J(1) = 1 | ||
| J(n) = J(n - 1) + 2 * J(n - 2) | ||
|
|
||
| The first terms are 0, 1, 1, 3, 5, 11, 21, 43, 85, 171, 341, ... | ||
|
|
||
| They also have the closed form J(n) = (2 ** n - (-1) ** n) / 3, which is used | ||
| below to cross-check the iterative result. | ||
|
|
||
| Source: | ||
| https://en.wikipedia.org/wiki/Jacobsthal_number | ||
| https://oeis.org/A001045 | ||
| """ | ||
|
|
||
|
|
||
| def jacobsthal_number(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: |
||
| """ | ||
| Return the nth Jacobsthal number, counting from J(0) = 0. | ||
|
|
||
| :param n: index of the Jacobsthal number, must be a non-negative integer | ||
| :return: the nth Jacobsthal number | ||
|
|
||
| >>> jacobsthal_number(0) | ||
| 0 | ||
| >>> jacobsthal_number(1) | ||
| 1 | ||
| >>> jacobsthal_number(5) | ||
| 11 | ||
| >>> jacobsthal_number(10) | ||
| 341 | ||
| >>> jacobsthal_number(64) | ||
| 6148914691236517205 | ||
| >>> all( | ||
| ... jacobsthal_number(i) == (2**i - (-1) ** i) // 3 for i in range(100) | ||
| ... ) | ||
| True | ||
| >>> jacobsthal_number(-1) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: n must be a non-negative integer, got -1 | ||
| >>> jacobsthal_number(2.5) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: n must be a non-negative integer, got 2.5 | ||
| >>> jacobsthal_number("3") | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: n must be a non-negative integer, got '3' | ||
| """ | ||
| if not isinstance(n, int) or isinstance(n, bool) or n < 0: | ||
| msg = f"n must be a non-negative integer, got {n!r}" | ||
| raise ValueError(msg) | ||
|
|
||
| previous, current = 0, 1 | ||
| for _ in range(n): | ||
| previous, current = current, current + 2 * previous | ||
| return previous | ||
|
|
||
|
|
||
| def jacobsthal_sequence(length: int) -> list[int]: | ||
| """ | ||
| Return the first `length` Jacobsthal numbers as a list. | ||
|
|
||
| :param length: how many terms to return, must be a non-negative integer | ||
| :return: list of the first `length` Jacobsthal numbers | ||
|
|
||
| >>> jacobsthal_sequence(0) | ||
| [] | ||
| >>> jacobsthal_sequence(1) | ||
| [0] | ||
| >>> jacobsthal_sequence(11) | ||
| [0, 1, 1, 3, 5, 11, 21, 43, 85, 171, 341] | ||
| >>> jacobsthal_sequence(-3) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: length must be a non-negative integer, got -3 | ||
| >>> jacobsthal_sequence(4.0) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: length must be a non-negative integer, got 4.0 | ||
| """ | ||
| if not isinstance(length, int) or isinstance(length, bool) or length < 0: | ||
| msg = f"length must be a non-negative integer, got {length!r}" | ||
| raise ValueError(msg) | ||
|
|
||
| sequence = [] | ||
| previous, current = 0, 1 | ||
| for _ in range(length): | ||
| sequence.append(previous) | ||
| previous, current = current, current + 2 * previous | ||
| return sequence | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import doctest | ||
|
|
||
| doctest.testmod() | ||
Oops, something went wrong.
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.
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