Repository navigation
Add Woodall numbers to maths/special_numbers #15777
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
Open
Alok-Yadav25
wants to merge
1
commit into
TheAlgorithms:master
Choose a base branch
from
Alok-Yadav25:add-woodall-numbers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,185 @@ | ||
| """ | ||
| Woodall Number | ||
|
|
||
| In number theory, a Woodall number (also known as a Cullen number of the second | ||
| kind) is any natural number of the form: | ||
|
|
||
| W_n = n * 2^n - 1 | ||
|
|
||
| for any integer n >= 1. | ||
|
|
||
| The first few Woodall numbers are: | ||
| 1, 7, 23, 63, 159, 383, 895, 2047, 4607, 10239, ... | ||
|
|
||
| They also satisfy the recurrence relation: | ||
| W_1 = 1 | ||
| W_(n + 1) = 2 * W_n + 2^(n + 1) + 1 | ||
|
|
||
| Woodall numbers were first studied by Allan J. C. Cullen in 1905 and later | ||
| investigated in detail by Herbert J. Woodall in 1917. | ||
|
|
||
| References: | ||
| https://en.wikipedia.org/wiki/Woodall_number | ||
| https://oeis.org/A003261 | ||
| """ | ||
|
|
||
|
|
||
| def woodall_number(n: int) -> int: | ||
| """ | ||
| Return the nth Woodall number: n * 2^n - 1. | ||
|
|
||
| :param n: index of the Woodall number, must be a positive integer (n >= 1) | ||
| :return: the nth Woodall number | ||
|
|
||
| >>> woodall_number(1) | ||
| 1 | ||
| >>> woodall_number(2) | ||
| 7 | ||
| >>> woodall_number(3) | ||
| 23 | ||
| >>> woodall_number(4) | ||
| 63 | ||
| >>> woodall_number(5) | ||
| 159 | ||
| >>> woodall_number(10) | ||
| 10239 | ||
| >>> woodall_number(20) | ||
| 20971519 | ||
| >>> all( | ||
| ... woodall_number(i) == i * (1 << i) - 1 for i in range(1, 100) | ||
| ... ) | ||
| True | ||
| >>> all( | ||
| ... woodall_number(i + 1) == 2 * woodall_number(i) + (1 << (i + 1)) + 1 | ||
| ... for i in range(1, 50) | ||
| ... ) | ||
| True | ||
| >>> woodall_number(0) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: n must be a positive integer (n >= 1), got 0 | ||
| >>> woodall_number(-5) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: n must be a positive integer (n >= 1), got -5 | ||
| >>> woodall_number(2.5) | ||
| Traceback (most recent call last): | ||
| ... | ||
| TypeError: n must be an integer, got 2.5 | ||
| >>> woodall_number("3") | ||
| Traceback (most recent call last): | ||
| ... | ||
| TypeError: n must be an integer, got '3' | ||
| >>> woodall_number(True) | ||
| Traceback (most recent call last): | ||
| ... | ||
| TypeError: n must be an integer, got True | ||
| """ | ||
| if not isinstance(n, int) or isinstance(n, bool): | ||
| msg = f"n must be an integer, got {n!r}" | ||
| raise TypeError(msg) | ||
| if n < 1: | ||
| msg = f"n must be a positive integer (n >= 1), got {n!r}" | ||
| raise ValueError(msg) | ||
|
|
||
| return (n << n) - 1 | ||
|
|
||
|
|
||
| def is_woodall_number(number: int) -> bool: | ||
| """ | ||
| Check whether a given integer is a Woodall number. | ||
|
|
||
| :param number: integer to test | ||
| :return: True if number is a Woodall number, False otherwise | ||
|
|
||
| >>> is_woodall_number(1) | ||
| True | ||
| >>> is_woodall_number(7) | ||
| True | ||
| >>> is_woodall_number(23) | ||
| True | ||
| >>> is_woodall_number(63) | ||
| True | ||
| >>> is_woodall_number(159) | ||
| True | ||
| >>> is_woodall_number(10239) | ||
| True | ||
| >>> is_woodall_number(0) | ||
| False | ||
| >>> is_woodall_number(-7) | ||
| False | ||
| >>> is_woodall_number(10) | ||
| False | ||
| >>> is_woodall_number(100) | ||
| False | ||
| >>> is_woodall_number(6.5) | ||
| Traceback (most recent call last): | ||
| ... | ||
| TypeError: number must be an integer, got 6.5 | ||
| >>> is_woodall_number("7") | ||
| Traceback (most recent call last): | ||
| ... | ||
| TypeError: number must be an integer, got '7' | ||
| >>> is_woodall_number(True) | ||
| Traceback (most recent call last): | ||
| ... | ||
| TypeError: number must be an integer, got True | ||
| """ | ||
| if not isinstance(number, int) or isinstance(number, bool): | ||
| msg = f"number must be an integer, got {number!r}" | ||
| raise TypeError(msg) | ||
| if number < 1: | ||
| return False | ||
|
|
||
| k = 1 | ||
| while True: | ||
| woodall = woodall_number(k) | ||
| if woodall == number: | ||
| return True | ||
| if woodall > number: | ||
| return False | ||
| k += 1 | ||
|
|
||
|
|
||
| def woodall_sequence(length: int) -> list[int]: | ||
| """ | ||
| Return the first `length` Woodall numbers as a list. | ||
|
|
||
| :param length: how many terms to return, must be a non-negative integer | ||
| :return: list of the first `length` Woodall numbers | ||
|
|
||
| >>> woodall_sequence(0) | ||
| [] | ||
| >>> woodall_sequence(1) | ||
| [1] | ||
| >>> woodall_sequence(5) | ||
| [1, 7, 23, 63, 159] | ||
| >>> woodall_sequence(10) | ||
| [1, 7, 23, 63, 159, 383, 895, 2047, 4607, 10239] | ||
| >>> woodall_sequence(-1) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: length must be a non-negative integer, got -1 | ||
| >>> woodall_sequence(3.5) | ||
| Traceback (most recent call last): | ||
| ... | ||
| TypeError: length must be an integer, got 3.5 | ||
| >>> woodall_sequence(True) | ||
| Traceback (most recent call last): | ||
| ... | ||
| TypeError: length must be an integer, got True | ||
| """ | ||
| if not isinstance(length, int) or isinstance(length, bool): | ||
| msg = f"length must be an integer, got {length!r}" | ||
| raise TypeError(msg) | ||
| if length < 0: | ||
| msg = f"length must be a non-negative integer, got {length!r}" | ||
| raise ValueError(msg) | ||
|
|
||
| return [woodall_number(i) for i in range(1, length + 1)] | ||
|
|
||
|
|
||
| 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