A number is called happy if it leads to 1 after a sequence of steps wherein each step number is replaced by the sum of squares of its digit that is if we start with Happy Number and keep replacing it with digits square sum, we reach 1.
Input: n = 19 Output: True 19 is Happy Number, 1^2 + 9^2 = 82 8^2 + 2^2 = 68 6^2 + 8^2 = 100 1^2 + 0^2 + 0^2 = 1 As we reached to 1, 19 is a Happy Number. Input: n = 20 Output: False
A number will not be a Happy Number when it makes a loop in its sequence that is it touches a number in sequence which already been touched. So to check whether a number is happy or not, we can keep a set, if the same number occurs again we flag result as not happy. A simple function on the above approach can be written as below –
We can solve this problem without using extra space and that technique can be used in some other similar problems also. If we treat every number as a node and replacement by square sum digit as a link, then this problem is same as finding a loop in a linklist :
So as a proposed solution from the above link, we will keep two numbers slow and fast both initialize from a given number, slow is replaced one step at a time and fast is replaced two steps at a time. If they meet at 1, then the given number is Happy Number otherwise not.
13 is a Happy Number
Another approach for solving this problem using no extra space.
A number cannot be a happy number if, at any step, the sum of the square of digits obtained is a single-digit number except 1 or 7. This is because 1 and 7 are the only single-digit happy numbers. Using this information, we can develop an approach as shown in the code below –
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks’ main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Happy Numbers
- Number of factors of very large number N modulo M where M is any prime number
- Find the largest number smaller than integer N with maximum number of set bits
- Count number of trailing zeros in Binary representation of a number using Bitset
- Find minimum number to be divided to make a number a perfect square
- Count the number of walks of length N where cost of each walk is equal to a given number
- Count number of triplets with product equal to given number with duplicates allowed
- Number of ways to split a binary number such that every part is divisible by 2
- Number of ways to calculate a target number using only array elements
- Minimum divisor of a number to make the number perfect cube
- Find a number which give minimum sum when XOR with every number of array of integers
- Total number of possible Binary Search Trees using Catalan Number
- Number of ways to divide a given number as a set of integers in decreasing order
- Number of times the largest perfect square number can be subtracted from N
- Find smallest possible Number from a given large Number with same count of digits