Write an algorithm to determine if a number n
is happy.
A happy number is a number defined by the following process:
Return true
if n
is a happy number, and false
if not.
Example 1:
Input: n = 19
Output: true
Explanation:
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
Example 2:
Input: n = 2
Output: false
Constraints:
1 <= n <= 2^31 - 1
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to determining if a number is a happy number is to repeatedly calculate the sum of the squares of its digits. We continue this process, checking if we reach 1 (meaning it's a happy number) or if we enter a cycle (meaning it's not).
Here's how the algorithm would work step-by-step:
def is_happy_number(number):
seen_numbers = set()
while number != 1:
# Check if we've entered a cycle.
if number in seen_numbers:
return False
seen_numbers.add(number)
sum_of_squared_digits = 0
temporary_number = number
# Calculate the sum of squared digits.
while temporary_number > 0:
digit = temporary_number % 10
sum_of_squared_digits += digit ** 2
temporary_number //= 10
number = sum_of_squared_digits
return True
A happy number eventually reaches 1 after repeatedly replacing it with the sum of the squares of its digits. The optimal approach detects cycles to avoid infinite loops and determine if a number is happy efficiently.
Here's how the algorithm would work step-by-step:
def is_happy(number) -> bool:
seen_numbers = set()
while number != 1:
sum_of_squared_digits = 0
temp_number = number
while temp_number > 0:
digit = temp_number % 10
sum_of_squared_digits += digit ** 2
temp_number //= 10
number = sum_of_squared_digits
# Cycle detection to identify unhappy numbers
if number in seen_numbers:
return False
# Keep track of checked numbers.
seen_numbers.add(number)
# If the number eventually becomes 1
return True
Case | How to Handle |
---|---|
Input is zero | Return false, since zero is not a positive integer and violates problem conditions. |
Input is 1 | Return true immediately as 1 is already a happy number by definition. |
Input is a very large number that potentially causes an infinite loop | Use a set to track previously seen numbers and return false if a number repeats, indicating a cycle. |
Input results in a very long sequence of sums before either reaching 1 or a cycle | The set tracking ensures that long sequences are handled without exceeding memory limits significantly. |
Integer overflow when squaring digits of a large number. | Consider using a larger integer type if the sum of squares could exceed the maximum value of the default integer type, or modular arithmetic if appropriate. |
Negative Input | Return false, since the problem specifies a positive integer. |
The input consists of all 9s (e.g., 99999) | This tests the performance with a number that yields larger intermediate sums before potentially looping or resolving to 1, and the cycle detection should appropriately terminate. |
Input leads to known unhappy cycles (e.g., the 4, 16, 37, 58, 89, 145, 42, 20 cycle) | The set tracking previously seen numbers should correctly identify and handle these cycles. |