Title Description:
Write an algorithm to determine if a number is a "happy number".
A number is the definition of happiness: For a positive integer, each time the number is replaced with the sum of the squares of the number in each of his positions, and then the process repeats until the number becomes 1, or the infinite loop but does not change to 1. If it can become 1, then this number is the number of happiness.
Have you ever encountered this problem in a real interview? Yes
Sample Example
19 is a happy number.
1^2 + 9^2 = 828^2 + 2^2 = 686^2 + 8^2 = 1001^2 + 0^2 + 0^2 = 1
labelMath Hash Table
Topic Analysis:
Converts the number n bitwise into a list, and then loops the sum of the squares of the elements, only to the result n==1 or n ==4;
Recommended Baidu happy number of circulation structure.
Non-happy numbers are always entered in the following repeating series:
4→16→37→58→89→145→42→20→4
For example non-happy number 8, 14:
8→64→52→29→85→89→145→42→20→4
14→17→50→25→29→85→89→145→42→20→4
For example Happy number 7:
7→49→97→130→10→1
In the decimal, the number of pleasures within 100 (Oeis in the series A00770): 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100.
Source:
Class solution: # @param {int} n an integer # @return {Boolean} True if the is a happy number or false def is Happy (self, N): # Write Your code here if n was none:return False while n! = 1 and N! = 4: nums = List (str (n)) n = 0 for i in Nums: n + = int (i) **2 # Loop End, return result if n = = 1:return True if n = = 4:return False
Lintcode Python Simple class topic 488. Happy number