Skip to content

Commit d938f9e

Browse files
committed
doc: add comments and README
1 parent ea09be9 commit d938f9e

File tree

2 files changed

+110
-7
lines changed

2 files changed

+110
-7
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+
Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,57 @@
11
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+
"""
215
slow = n
316
fast = squared_digits_sum(n)
417

5-
while fast != slow or n != 1:
18+
while fast != slow:
619
slow = squared_digits_sum(slow)
720
fast = squared_digits_sum(squared_digits_sum(fast))
821

9-
if fast == slow:
10-
return False
11-
return True
12-
22+
return slow == 1
23+
1324
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+
"""
1434
digits_sum = 0
1535
while n > 0:
16-
n, digit = divmod(n,10)
36+
n, digit = divmod(n, 10)
1737
digits_squared = digit * digit
1838
digits_sum += digits_squared
19-
return digits_sum
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)