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.

&nbsp;
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


&nbsp;
Constraints:


	1 &lt;= n &lt;= 231 - 1


class Solution(object):
    def isHappy(self, n):
        hset = set()
        while n != 1:
            if n in hset: return False
            hset.add(n)
            n = sum([int(i) ** 2 for i in str(n)])
        else:
            return True
class Solution {
    public boolean isHappy(int n) {
        // Create a hash set...
        Set<Integer> hset = new HashSet<Integer>();
        // If the number is not in the HashSet, we should add it...
        while (hset.add(n)) {
            // Initialize the total...
            int total = 0;
            // Create a while loop...
            while (n > 0) {
                // Process to get happy number...
                // We use division and modulus operators to repeatedly take digits off the number until none remain...
                // Then squaring each removed digit and adding them together...
                total += (n % 10) * (n % 10);
                n /= 10;
                // Each new converted number must not had occurred before...
            }
            // If total is equal to 1 return true.
            if (total == 1)
                return true;
            // Insert the current number into the set s...
            // Replace the current number with total of the square of its digits.
            else
                n = total;
        }
        // If current number is already in the HashSet, that means we're in a cycle and we should return false..
        return false;
    }
}
class Solution {
public:
    bool isHappy(int n) {
        // Create a set...
        set<int> hset;
        while(hset.count(n) == 0) {
            // If total is equal to 1 return true.
            if(n == 1)
                return true;
            // Insert the current number in hset...
            hset.insert(n);
            // Initialize the total...
            int total=0;
            // Create a while loop...
            while(n) {
                // Process to get happy number...
                // We use division and modulus operators to repeatedly take digits off the number until none remain...
                // Then squaring each removed digit and adding them together.
                total += (n % 10) * (n % 10);
                n /= 10;
                // Each new converted number must not had occurred before...
            }
            // Insert the current number into the set s...
            // Replace the current number with total of the square of its digits.
            n = total;
        }
        // If current number is already in the HashSet, that means we're in a cycle and we should return false..
        return false;
    }
};
var isHappy = function(n) {
    if(n<10){
        if(n === 1 || n === 7){
            return true
        }
        return false
    }
    let total = 0
    while(n>0){
        let sq = n % 10
        total += sq**2
        n -= sq
        n /= 10
    }
    if(total === 1){
        return true
    }
    return isHappy(total)
};
Happy Number