- Notifications
You must be signed in to change notification settings - Fork12
feat: add happy number#364
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
65 changes: 65 additions & 0 deletionsarrays_and_strings/happy_number/README.md
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,65 @@ | ||
## **Problem Statement** | ||
Write an algorithm to determine if a number `n` is a happy number. | ||
We use the following process to check if a given number is a happy number: | ||
- Starting with the given number n, replace the number with the sum of the squares of its digits. | ||
- Repeat the process until: | ||
- The number equals 1, which will depict that the given number n is a happy number. | ||
- It enters a cycle, which will depict that the given number n is not a happy number. | ||
Return TRUE if `n` is a happy number, and FALSE if not. | ||
### Constraints | ||
> 1 ≤ n ≤ 2<sup>31</sup> - 1 | ||
## **Examples** | ||
### Example 1: Determining a Happy Number | ||
**Input:** `n = 19` | ||
**Process:** | ||
1. Start with `n = 19`. | ||
2. Calculate the sum of the squares of its digits: $1^2 + 9^2 = 1 + 81 = 82$. | ||
3. Replace 19 with 82. | ||
4. Calculate the sum of the squares of the digits of 82: $8^2 + 2^2 = 64 + 4 = 68$. | ||
5. Replace 82 with 68. | ||
6. Calculate the sum of the squares of the digits of 68: $6^2 + 8^2 = 36 + 64 = 100$. | ||
7. Replace 68 with 100. | ||
8. Calculate the sum of the squares of the digits of 100: $1^2 + 0^2 + 0^2 = 1 + 0 + 0 = 1$. | ||
Since the process reaches 1, **19 is a happy number**. | ||
**Output:** `TRUE` | ||
### Example 2: Determining a Non-Happy Number | ||
**Input:** `n = 20` | ||
**Process:** | ||
1. Start with `n = 20`. | ||
2. Calculate the sum of the squares of its digits: $2^2 + 0^2 = 4 + 0 = 4$. | ||
3. Replace 20 with 4. | ||
4. Calculate the sum of the squares of the digits of 4: $4^2 = 16$. | ||
5. Replace 4 with 16. | ||
6. Calculate the sum of the squares of the digits of 16: $1^2 + 6^2 = 1 + 36 = 37$. | ||
7. Replace 16 with 37. | ||
8. Calculate the sum of the squares of the digits of 37: $3^2 + 7^2 = 9 + 49 = 58$. | ||
9. Replace 37 with 58. | ||
10. Calculate the sum of the squares of the digits of 58: $5^2 + 8^2 = 25 + 64 = 89$. | ||
11. Replace 58 with 89. | ||
12. Calculate the sum of the squares of the digits of 89: $8^2 + 9^2 = 64 + 81 = 145$. | ||
13. Replace 89 with 145. | ||
14. Calculate the sum of the squares of the digits of 145: $1^2 + 4^2 + 5^2 = 1 + 16 + 25 = 42$. | ||
15. Replace 145 with 42. | ||
16. Calculate the sum of the squares of the digits of 42: $4^2 + 2^2 = 16 + 4 = 20$. | ||
The process returns to 20, indicating a cycle. **20 is not a happy number**. | ||
**Output:** `FALSE` | ||
Empty file addedarrays_and_strings/happy_number/__init__.py
Empty file.
57 changes: 57 additions & 0 deletionsarrays_and_strings/happy_number/happy_number.py
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 @@ | ||
def happy_number(n: int) -> bool: | ||
""" | ||
Determine if a number is a happy number. | ||
A happy number is defined as a number which eventually reaches 1 when replaced by the sum of the square of each digit. | ||
If it loops endlessly in a cycle that does not include 1, it is not a happy number. | ||
Parameters: | ||
n (int): The number to be checked. | ||
Returns: | ||
bool: True if the number is a happy number, False otherwise. | ||
""" | ||
slow = n | ||
fast = squared_digits_sum(n) | ||
while fast != slow: | ||
slow = squared_digits_sum(slow) | ||
fast = squared_digits_sum(squared_digits_sum(fast)) | ||
return slow == 1 | ||
def squared_digits_sum(n: int) -> int: | ||
""" | ||
Calculate the sum of the squares of the digits of a number. | ||
Parameters: | ||
n (int): The number whose digits are to be squared and summed. | ||
Returns: | ||
int: The sum of the squares of the digits of the number. | ||
""" | ||
digits_sum = 0 | ||
while n > 0: | ||
n, digit = divmod(n, 10) | ||
digits_squared = digit * digit | ||
digits_sum += digits_squared | ||
return digits_sum | ||
# Approach and Reasoning: | ||
# ----------------------- | ||
# - The function uses Floyd's Cycle Detection Algorithm (also known as the tortoise and hare algorithm) to detect cycles. Also known as fast and slow pointers. | ||
# - Two pointers, `slow` and `fast`, are used to traverse the sequence of numbers generated by repeatedly replacing the number with the sum of the squares of its digits. | ||
# - `slow` moves one step at a time (computes the sum of squares once), while `fast` moves two steps at a time (computes the sum of squares twice). | ||
# - If there is a cycle (i.e., the number is not happy), `slow` and `fast` will eventually meet at the same number. | ||
# - If the number is happy, the sequence will eventually reach 1, and the loop will terminate. | ||
# Time Complexity: | ||
# ---------------- | ||
# - The time complexity is O(log n), where n is the input number. | ||
# - The time complexity is O(log_{10}(n)), where n is the input number. This complexity arises from the `squared_digits_sum` function, | ||
# as the number of digits in n is proportional to log_{10}(n), and each digit is processed in constant time. | ||
# Space Complexity: | ||
# ----------------- | ||
# - The space complexity is O(1) because only a constant amount of extra space is used for variables and function calls. |
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.