0% completed
Solution: Happy Number
Problem Statement
Any number will be called a happy number if, after repeatedly replacing it with a number equal to the sum of the square of all of its digits, leads us to the number 1. All other (not-happy) numbers will never reach 1. Instead, they will be stuck in a cycle of numbers that does not include 1.
Given a positive number n, return true if it is a happy number otherwise return false.
Example 1:
Input: 23
Output: true (23 is a happy number)
Explanations: Here are the steps to find out that 23 is a happy number:2^2 +3^2 = 4 + 9 = 13
.....
.....
.....
anhquan.tranle0601
· 3 years ago
If num = 10, in the loop body the condition slow == fast will be true and the function return False, which is incorrect.
We should have a condition to check if fast == 1 before checking slow == fast