- 39.8%

https://leetcode.com/problems/happy-number/description/

Write an algorithm to determine if a number is “happy”.

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

Example: 19 is a happy number

1 | 1^2 + 9^2 = 82 |

方法一：

从开始，遍历过的放入set，如果再出现一次，则肯定为false，如果到1，返回true。

1 | class Solution { |

另一种实现：

把一样放入table里，如果出现过，就判断是否等于1. 1的下一个还是1.

1 | class Solution { |

方法二：

比方法一差一些，因为没有保存，每次都去判断

1 | bool isHappy(int n) { |

Earlier posts gave the algorithm but did not explain why it is valid mathematically, and this is what this post is about: present a “short” mathematical proof.

First of all, it is easy to argue that starting from a number I, if some value - say a - appears again during the process after k steps, the initial number I cannot be a happy number. Because a will continuously become a after every k steps.

Therefore, as long as we can show that there is a loop after running the process continuously, the number is not a happy number.

There is another detail not clarified yet: For any non-happy number, will it definitely end up with a loop during the process? This is important, because it is possible for a non-happy number to follow the process endlessly while having no loop.

To show that a non-happy number will definitely generate a loop, we only need to show that for any non-happy number, all outcomes during the process are bounded by some large but finite integer N. If all outcomes can only be in a finite set (2,N], and since there are infinitely many outcomes for a non-happy number, there has to be at least one duplicate, meaning a loop!

Suppose after a couple of processes, we end up with a large outcome O1 with D digits where D is kind of large, say D>=4, i.e., O1 > 999 (If we cannot even reach such a large outcome, it means all outcomes are bounded by 999 ==> loop exists). We can easily see that after processing O1, the new outcome O2 can be at most 9^2*D < 100D, meaning that O2 can have at most 2+d(D) digits, where d(D) is the number of digits D have. It is obvious that 2+d(D) < D. We can further argue that O1 is the maximum (or boundary) of all outcomes afterwards. This can be shown by contradictory: Suppose after some steps, we reach another large number O3 > O1. This means we process on some number W <= 999 that yields O3. However, this cannot happen because the outcome of W can be at most 9^2*3 < 300 < O1.

Done.

Please leave your comment if any question or suggestion.

#### cpp

I use three solutions: 1. hash table(8ms) 2. List circle detect method(4ms) 3.circle 1 and circle 4.

Here is the url:https://en.wikipedia.org/wiki/Happy_number Based on the fact that circle of 1 is happy, circle of 4 is unhappy.

Solution3:

1 | bool isHappy(int n) { |

Solution1:

1 | class Solution { |

Solution2:

1 | class Solution { |

#### python

Solution 1:

64ms, 42.28%, June.17th, 2016

https://leetcode.com/discuss/47623/my-python-solution

1 | class Solution(object): |

#### c

Solution 1:

0ms, 44.72%, June.17th, 2016

https://leetcode.com/discuss/33014/4ms-5-line-c-code

Using fact all numbers in [2, 6] are not happy (and all not happy numbers end on a cycle that hits this interval):

1 | bool isHappy(int n) { |

I see the majority of those posts use hashset to record values. Actually, we can simply adapt the Floyd Cycle detection algorithm. I believe that many people have seen this in the Linked List Cycle detection problem. The following is my code:

1 | int digitSquareSum(int n) { |

#### java

The idea is to use one hash set to record sum of every digit square of every number occurred. Once the current sum cannot be added to set, return false; once the current sum equals 1, return true;

1 | public class Solution { |

https://discuss.leetcode.com/topic/12742/o-1-space-java-solution

1 | public class Solution { |