Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c7a90b7

Browse files
Merge pull request #364 from MoigeMatino/happy-number
feat: add happy number
2 parents e2b68d2 + d938f9e commit c7a90b7

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
## **Problem Statement**
2+
3+
Write an algorithm to determine if a number `n` is a happy number.
4+
5+
We use the following process to check if a given number is a happy number:
6+
7+
- Starting with the given number n, replace the number with the sum of the squares of its digits.
8+
- Repeat the process until:
9+
- The number equals 1, which will depict that the given number n is a happy number.
10+
- It enters a cycle, which will depict that the given number n is not a happy number.
11+
12+
Return TRUE if `n` is a happy number, and FALSE if not.
13+
14+
### Constraints
15+
16+
> 1 ≤ n ≤ 2<sup>31</sup> - 1
17+
18+
## **Examples**
19+
20+
### Example 1: Determining a Happy Number
21+
22+
**Input:** `n = 19`
23+
24+
**Process:**
25+
26+
1. Start with `n = 19`.
27+
2. Calculate the sum of the squares of its digits: 1ドル^2 + 9^2 = 1 + 81 = 82$.
28+
3. Replace 19 with 82.
29+
4. Calculate the sum of the squares of the digits of 82: 8ドル^2 + 2^2 = 64 + 4 = 68$.
30+
5. Replace 82 with 68.
31+
6. Calculate the sum of the squares of the digits of 68: 6ドル^2 + 8^2 = 36 +たす 64 = 100$.
32+
7. Replace 68 with 100.
33+
8. Calculate the sum of the squares of the digits of 100: 1ドル^2 + 0^2 + 0^2 = 1 +たす 0 +たす 0 = 1$.
34+
35+
Since the process reaches 1, **19 is a happy number**.
36+
37+
**Output:** `TRUE`
38+
39+
### Example 2: Determining a Non-Happy Number
40+
41+
**Input:** `n = 20`
42+
43+
**Process:**
44+
45+
1. Start with `n = 20`.
46+
2. Calculate the sum of the squares of its digits: 2ドル^2 + 0^2 = 4 + 0 = 4$.
47+
3. Replace 20 with 4.
48+
4. Calculate the sum of the squares of the digits of 4: 4ドル^2 = 16$.
49+
5. Replace 4 with 16.
50+
6. Calculate the sum of the squares of the digits of 16: 1ドル^2 + 6^2 = 1 + 36 = 37$.
51+
7. Replace 16 with 37.
52+
8. Calculate the sum of the squares of the digits of 37: 3ドル^2 + 7^2 = 9 + 49 = 58$.
53+
9. Replace 37 with 58.
54+
10. Calculate the sum of the squares of the digits of 58: 5ドル^2 + 8^2 = 25 + 64 = 89$.
55+
11. Replace 58 with 89.
56+
12. Calculate the sum of the squares of the digits of 89: 8ドル^2 + 9^2 = 64 +たす 81 = 145$.
57+
13. Replace 89 with 145.
58+
14. Calculate the sum of the squares of the digits of 145: 1ドル^2 + 4^2 + 5^2 = 1 +たす 16 +たす 25 = 42$.
59+
15. Replace 145 with 42.
60+
16. Calculate the sum of the squares of the digits of 42: 4ドル^2 + 2^2 = 16 + 4 = 20$.
61+
62+
The process returns to 20, indicating a cycle. **20 is not a happy number**.
63+
64+
**Output:** `FALSE`
65+

‎arrays_and_strings/happy_number/__init__.py

Whitespace-only changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
def happy_number(n: int) -> bool:
2+
"""
3+
Determine if a number is a happy number.
4+
5+
A happy number is defined as a number which eventually reaches 1 when replaced by the sum of the square of each digit.
6+
If it loops endlessly in a cycle that does not include 1, it is not a happy number.
7+
8+
Parameters:
9+
n (int): The number to be checked.
10+
11+
Returns:
12+
bool: True if the number is a happy number, False otherwise.
13+
14+
"""
15+
slow = n
16+
fast = squared_digits_sum(n)
17+
18+
while fast != slow:
19+
slow = squared_digits_sum(slow)
20+
fast = squared_digits_sum(squared_digits_sum(fast))
21+
22+
return slow == 1
23+
24+
def squared_digits_sum(n: int) -> int:
25+
"""
26+
Calculate the sum of the squares of the digits of a number.
27+
28+
Parameters:
29+
n (int): The number whose digits are to be squared and summed.
30+
31+
Returns:
32+
int: The sum of the squares of the digits of the number.
33+
"""
34+
digits_sum = 0
35+
while n > 0:
36+
n, digit = divmod(n, 10)
37+
digits_squared = digit * digit
38+
digits_sum += digits_squared
39+
return digits_sum
40+
41+
# Approach and Reasoning:
42+
# -----------------------
43+
# - 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.
44+
# - 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.
45+
# - `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).
46+
# - If there is a cycle (i.e., the number is not happy), `slow` and `fast` will eventually meet at the same number.
47+
# - If the number is happy, the sequence will eventually reach 1, and the loop will terminate.
48+
49+
# Time Complexity:
50+
# ----------------
51+
# - The time complexity is O(log n), where n is the input number.
52+
# - The time complexity is O(log_{10}(n)), where n is the input number. This complexity arises from the `squared_digits_sum` function,
53+
# as the number of digits in n is proportional to log_{10}(n), and each digit is processed in constant time.
54+
55+
# Space Complexity:
56+
# -----------------
57+
# - The space complexity is O(1) because only a constant amount of extra space is used for variables and function calls.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /