-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
algorithm: Add juggler sequence #7985
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 8 commits into
TheAlgorithms:master
from
itsAkshayDubey:algorithm-juggler-sequence
Nov 18, 2022
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aab776c
feat: Add juggler sequence
itsAkshayDubey d9c8d06
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f67bf3e
refactor: Remove temp variable
itsAkshayDubey a0a875a
refactor: Change error type for negative numbers
itsAkshayDubey 7448476
refactor: Remove # doctest: +NORMALIZE_WHITESPACE
itsAkshayDubey dc5db72
refactor: Remove int typecasting
itsAkshayDubey 20da7a3
test: Add unit tests for n=10 and n=25
itsAkshayDubey 2aa506f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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,57 @@ | ||
""" | ||
== Juggler Sequence == | ||
Juggler sequence start with any positive integer n. The next term is | ||
obtained as follows: | ||
If n term is even, the next term is floor value of square root of n . | ||
If n is odd, the next term is floor value of 3 time the square root of n. | ||
|
||
https://en.wikipedia.org/wiki/Juggler_sequence | ||
""" | ||
|
||
# Author : Akshay Dubey (https://github.com/itsAkshayDubey) | ||
import math | ||
|
||
|
||
def juggler_sequence(number: int) -> list[int]: | ||
""" | ||
>>> juggler_sequence(0) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Input value of [number=0] must be a positive integer | ||
>>> juggler_sequence(1) | ||
[1] | ||
>>> juggler_sequence(2) | ||
[2, 1] | ||
>>> juggler_sequence(3) | ||
[3, 5, 11, 36, 6, 2, 1] | ||
>>> juggler_sequence(5) | ||
[5, 11, 36, 6, 2, 1] | ||
>>> juggler_sequence(6.0) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: Input value of [number=6.0] must be an integer | ||
>>> juggler_sequence(-1) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Input value of [number=-1] must be a positive integer | ||
""" | ||
if not isinstance(number, int): | ||
raise TypeError(f"Input value of [number={number}] must be an integer") | ||
if number < 1: | ||
raise ValueError(f"Input value of [number={number}] must be a positive integer") | ||
sequence = [number] | ||
while number != 1: | ||
if number % 2 == 0: | ||
number = int(math.floor(math.sqrt(number))) | ||
else: | ||
number = int( | ||
math.floor(math.sqrt(number) * math.sqrt(number) * math.sqrt(number)) | ||
) | ||
itsAkshayDubey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sequence.append(number) | ||
return sequence | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
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.