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: 12 + 92 = 82 82 + 22 = 68 62 + 82 = 100 12 + 02 + 02 = 1
Example 2:
Input: n = 2 Output: false
Constraints:
1 <= n <= 231 - 1When 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 way to solve this is to follow the number's path of transformations repeatedly and see where it leads. We'll keep doing this transformation until we either find the special number we're looking for, or we realize we're stuck in a repeating pattern that won't lead us there.
Here's how the algorithm would work step-by-step:
def is_happy(starting_number):
seen_numbers = set()
current_number = starting_number
while current_number != 1 and current_number not in seen_numbers:
# Mark the current number as seen to detect cycles
seen_numbers.add(current_number)
sum_of_squares = 0
# Calculate the sum of squares of digits
for digit_character in str(current_number):
digit_value = int(digit_character)
sum_of_squares += digit_value * digit_value
# Update the current number for the next iteration
current_number = sum_of_squares
# A number is happy if the process ends at 1
return current_number == 1To determine if a number is 'happy,' we repeatedly transform it by summing the squares of its digits. If this process eventually reaches 1, the number is happy. If it enters a cycle without reaching 1, it's not happy. The clever part is detecting these cycles efficiently.
Here's how the algorithm would work step-by-step:
def is_happy_number(starting_number):
seen_numbers = set()
current_number = starting_number
while current_number != 1:
# Detect cycles to prevent infinite loops when a number is not happy.
if current_number in seen_numbers:
return False
# Add the current number to our history to detect future cycles.
seen_numbers.add(current_number)
sum_of_squared_digits = 0
number_to_process = current_number
while number_to_process > 0:
digit = number_to_process % 10
sum_of_squared_digits += digit * digit
number_to_process //= 10
# Update the current number for the next iteration.
current_number = sum_of_squared_digits
# If the loop terminates, it means we reached 1, hence the number is happy.
return True| Case | How to Handle |
|---|---|
| Input is 1 | The number 1 is already the target happy number, so the function should return true immediately. |
| Input is a single-digit number other than 1 | Single-digit numbers will eventually lead to a cycle, and the algorithm should correctly detect this and return false. |
| Input causes a long sequence before reaching 1 or a cycle | The algorithm should be efficient enough to handle numbers that require many iterations without timing out. |
| Input enters a known cycle that does not include 1 | A mechanism to detect repeated numbers in the sequence is crucial to identify non-happy numbers and avoid infinite loops. |
| Extremely large input numbers | While the problem statement mentions positive integers, extremely large inputs could potentially lead to integer overflow if intermediate sums of squares exceed the language's integer limits. |
| All digits are the same (e.g., 222, 333) | These inputs should be processed normally; the algorithm should correctly sum the squares of their digits and proceed. |
| Numbers that result in a 0 after summing squares of digits | This scenario is impossible for positive integers since the square of any digit (0-9) is non-negative, and at least one digit must be non-zero for a positive integer. |
| Input with leading zeros (e.g., 007) | Input validation or standard integer parsing should handle this by treating it as a standard integer (e.g., 7). |