-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Happy number (new algorithm) #10864
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
Happy number (new algorithm) #10864
Changes from 8 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
3a2112c
Happy number (new algorithm)
Devadeut 7047bf5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 798774c
Update maths/special_numbers/happy_number.py
Devadeut 5ebf099
Update happy_number.py
Devadeut b07f7c8
Merge branch 'TheAlgorithms:master' into new
Devadeut 6c92c19
Update happy_number.py
Devadeut 24fdd2a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] aaa124c
Update happy_number.py
Devadeut b68ec00
Update happy_number.py
Devadeut bde4c22
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 21e5168
Update happy_number.py
Devadeut 715d563
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4cf19e9
Merge branch 'TheAlgorithms:master' into new
Devadeut 6c726c1
Update happy_number.py
Devadeut 17b6343
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f55247e
Update happy_number.py
Devadeut b9dde2d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 420e253
Update happy_number.py
cclauss 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,48 @@ | ||
def is_happy_number(number: int) -> bool: | ||
""" | ||
Check if a number is a happy number. | ||
https://en.wikipedia.org/wiki/Happy_number | ||
A happy number is defined by the following process: | ||
1. Starting with any positive integer, replace the number by | ||
the sum of the squares of its digits. | ||
2. Repeat the process until the number equals 1 (happy) or | ||
it loops endlessly in a cycle (not happy). | ||
|
||
Args: | ||
number (int or float): The number to check for happiness. | ||
|
||
Returns: | ||
bool: True if the number is a happy number, False otherwise. | ||
|
||
Examples: | ||
>>> is_happy_number(19) | ||
True | ||
>>> is_happy_number(4) | ||
False | ||
>>> is_happy_number(23) | ||
True | ||
>>> is_happy_number(0) | ||
False | ||
>>> is_happy_number(19.1) | ||
False | ||
>>> is_happy_number(-19) | ||
False | ||
>>> is_happy_number("Happy") | ||
False | ||
""" | ||
|
||
# Check if the input is an integer or a float | ||
if isinstance(number, (int)) and number > 0: | ||
# Create a set to store seen numbers and detect cycles | ||
Devadeut marked this conversation as resolved.
Show resolved
Hide resolved
|
||
seen = set() | ||
while number != 1 and number not in seen: | ||
seen.add(number) | ||
number = sum(int(digit) ** 2 for digit in str(number)) | ||
|
||
return number == 1 | ||
|
||
|
||
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.