# Happy Number

A number is called happy if it leads to 1 after a sequence of steps where in each step number is replaced by 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.
Examples :

```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
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

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 same number occurs again we flag result as not happy. A simple function on above approach can be written as below –

 `//  method return true if n is Happy Number ` `//  numSquareSum method is given in below detailed code snippet ` `int` `isHappyNumber(``int` `n) ` `{ ` `    ``set<``int``> st; ` `    ``while` `(1) ` `    ``{ ` `        ``n = numSquareSum(n); ` `        ``if` `(n == 1) ` `            ``return` `true``; ` `        ``if` `(st.find(n) != st.end()) ` `            ``return` `false``; ` `        ``st.insert(n); ` `    ``} ` `} `

We can solve this problem without using extra space and that technique can be used in some other similar problem 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 proposed solution from above link, we will keep two number slow and fast both initialize from 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.

## C++

 `//    C/C++ program to check a number is a Happy ` `//  number or not ` `#include ` `using` `namespace` `std; ` ` `  `// Utility method to return sum of square of ` `// digit of n ` `int` `numSquareSum(``int` `n) ` `{ ` `    ``int` `squareSum = 0; ` `    ``while` `(n) ` `    ``{ ` `        ``squareSum += (n % 10) * (n % 10); ` `        ``n /= 10; ` `    ``} ` `    ``return` `squareSum; ` `} ` ` `  `//    method return true if n is Happy number ` `bool` `isHappynumber(``int` `n) ` `{ ` `    ``int` `slow, fast; ` ` `  `    ``//    initialize slow and fast by n ` `    ``slow = fast = n; ` `    ``do` `    ``{ ` `        ``//    move slow number by one iteration ` `        ``slow = numSquareSum(slow); ` ` `  `        ``//    move fast number by two iteration ` `        ``fast = numSquareSum(numSquareSum(fast)); ` ` `  `    ``} ` `    ``while` `(slow != fast); ` ` `  `    ``//    if both number meet at 1, then return true ` `    ``return` `(slow == 1); ` `} ` ` `  `//    Driver code to test above methods ` `int` `main() ` `{ ` `    ``int` `n = 13; ` `    ``if` `(isHappynumber(n)) ` `        ``cout << n << ``" is a Happy number\n"``; ` `    ``else` `        ``cout << n << ``" is not a Happy number\n"``; ` `} `

## Java

 `//  Java program to check a number is a Happy ` `//  number or not ` ` `  `class` `GFG { ` `  `  `// Utility method to return sum of square of ` `// digit of n ` `static` `int` `numSquareSum(``int` `n) ` `{ ` `    ``int` `squareSum = ``0``; ` `    ``while` `(n!= ``0``) ` `    ``{ ` `        ``squareSum += (n % ``10``) * (n % ``10``); ` `        ``n /= ``10``; ` `    ``} ` `    ``return` `squareSum; ` `} ` `  `  `//  method return true if n is Happy number ` `static` `boolean` `isHappynumber(``int` `n) ` `{ ` `    ``int` `slow, fast; ` `  `  `    ``//  initialize slow and fast by n ` `    ``slow = fast = n; ` `    ``do` `    ``{ ` `        ``//  move slow number ` `        ``// by one iteration ` `        ``slow = numSquareSum(slow); ` `  `  `        ``//  move fast number ` `        ``// by two iteration ` `        ``fast = numSquareSum(numSquareSum(fast)); ` `  `  `    ``} ` `    ``while` `(slow != fast); ` `  `  `    ``//  if both number meet at 1, ` `    ``// then return true ` `    ``return` `(slow == ``1``); ` `} ` `  `  `//  Driver code to test above methods ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``13``; ` `    ``if` `(isHappynumber(n)) ` `        ``System.out.println(n +  ` `        ``" is a Happy number"``); ` `    ``else` `        ``System.out.println(n +  ` `        ``" is not a Happy number"``); ` `} ` `} `

## Python3

 `# Python3 program to check a number ` `# is a Happy number or not ` ` `  `# Utility method to return  ` `# sum of square of digit of n ` `def` `numSquareSum(n): ` `    ``squareSum ``=` `0``; ` `    ``while``(n): ` `        ``squareSum ``+``=` `(n ``%` `10``) ``*` `(n ``%` `10``); ` `        ``n ``=` `int``(n ``/` `10``); ` `    ``return` `squareSum; ` ` `  `# method return true if ` `# n is Happy number ` `def` `isHappynumber(n): ` ` `  `    ``# initialize slow  ` `    ``# and fast by n ` `    ``slow ``=` `n; ` `    ``fast ``=` `n; ` `    ``while``(``True``): ` `         `  `        ``# move slow number ` `        ``# by one iteration ` `        ``slow ``=` `numSquareSum(slow); ` ` `  `        ``# move fast number ` `        ``# by two iteration ` `        ``fast ``=` `numSquareSum(numSquareSum(fast)); ` `        ``if``(slow !``=` `fast): ` `            ``continue``; ` `        ``else``: ` `            ``break``; ` ` `  `    ``# if both number meet at 1,  ` `    ``# then return true ` `    ``return` `(slow ``=``=` `1``); ` ` `  `# Driver Code ` `n ``=` `13``; ` `if` `(isHappynumber(n)): ` `    ``print``(n , ``"is a Happy number"``); ` `else``: ` `    ``print``(n , ``"is not a Happy number"``); ` ` `  `# This code is contributed by mits `

## C#

 `// C# program to check a number ` `// is a Happy number or not ` `using` `System; ` `class` `GFG { ` ` `  `// Utility method to return  ` `// sum of square of digit of n ` `static` `int` `numSquareSum(``int` `n) ` `{ ` `    ``int` `squareSum = 0; ` `    ``while` `(n!= 0) ` `    ``{ ` `        ``squareSum += (n % 10) *  ` `                     ``(n % 10); ` `        ``n /= 10; ` `    ``} ` `    ``return` `squareSum; ` `} ` ` `  `// method return true if ` `// n is Happy number ` `static` `bool` `isHappynumber(``int` `n) ` `{ ` `    ``int` `slow, fast; ` ` `  `    ``// initialize slow and ` `    ``// fast by n ` `    ``slow = fast = n; ` `    ``do` `    ``{ ` `         `  `        ``// move slow number ` `        ``// by one iteration ` `        ``slow = numSquareSum(slow); ` ` `  `        ``// move fast number ` `        ``// by two iteration ` `        ``fast = numSquareSum(numSquareSum(fast)); ` ` `  `    ``} ` `    ``while` `(slow != fast); ` ` `  `    ``// if both number meet at 1, ` `    ``// then return true ` `    ``return` `(slow == 1); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `n = 13; ` `    ``if` `(isHappynumber(n)) ` `        ``Console.WriteLine(n +  ` `        ``" is a Happy number"``); ` `    ``else` `        ``Console.WriteLine(n +  ` `        ``" is not a Happy number"``); ` `} ` `} ` ` `  `// This code is contributed by anuj_67. `

## PHP

 ` `

Output :

```13 is a Happy Number
```

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 contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.