**Physical Address**

304 North Cardinal St.

Dorchester Center, MA 02124

Write an algorithm to determine if a number `n`

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

Return `true`

*if* `n`

*is a happy number, and* `false`

*if not*.

**Example 1:**

```
Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
```

**Example 2:**

```
Input: n = 2
Output: false
```

**Constraints:**

`1 <= n <= 2`

^{31}- 1

```
public boolean isHappy(int n) {
Set<Integer> inLoop = new HashSet<Integer>();
int squareSum,remain;
while (inLoop.add(n)) {
squareSum = 0;
while (n > 0) {
remain = n%10;
squareSum += remain*remain;
n /= 10;
}
if (squareSum == 1)
return true;
else
n = squareSum;
}
return false;
}
```

```
def isHappy(self, n):
mem = set()
while n != 1:
n = sum([int(i) ** 2 for i in str(n)])
if n in mem:
return False
else:
mem.add(n)
else:
return True
```

```
class Solution {
public:
bool isHappy(int n) {
unordered_set<int> tmp;
while(n != 1)
{
if(tmp.find(n) == tmp.end())
tmp.insert(n);
else
return false;
int sum = 0;
while(n != 0)
{
sum += pow(n % 10,2);
n = n / 10;
}
n = sum;
}
return true;
}
};
```

In our experience, we suggest you solve this Happy Number LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Happy Number LeetCode Solution

I hope this Happy Number LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

**More Coding Solutions >>**