Write an algorithm to determine if a number 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, and 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 numbers.

Example: 


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

Source: leetcode | Difficulty: EASY
Tags: Data structures

Amortized analysis

Mathematics

Starter code: class Solution:
    def isHappy(self, n: int) -> bool:
        

Solutions:
class Solution:

	def isHappy(self, n):
		former = set()
		while True:
			h = 0
			while n > 0:
				d = n % 10
				h += d * d
				n = n // 10
			if h == 1:
				return True
			elif h in former:
				return False
			n = h
			former.add(n)


class Solution:

	def isHappy(self, n):

		def next(n):
			res = 0
			while n > 0:
				(n, lsd) = (n // 10, n % 10)
				res += lsd * lsd
			return res
		seen = set()
		while True:
			seen.add(n)
			n = next(n)
			if n == 1:
				return True
			if n in seen:
				return False


class Solution:

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


class Solution:

	def isHappy(self, n):
		nums = set()
		while n != 1:
			if n in nums:
				return False
			nums.add(n)
			s = str(n)
			new_n = 0
			for c in s:
				new_n += int(c) * int(c)
			n = new_n
		return True


class Solution:

	def isHappy(self, n):
		if n < 0:
			return False
		if n == 1:
			return True
		my_set = set()
		while n not in my_set:
			my_set.add(n)
			sq_sum = 0
			while n > 0:
				remain = n % 10
				print(('r', remain))
				sq_sum += int(remain * remain)
				n = int(n / 10)
			print(sq_sum)
			if sq_sum == 1:
				return True
			else:
				n = sq_sum
		return False


class Solution:

	def isHappy(self, n):
		record = {}
		add = 0
		while add != 1:
			add = 0
			for s in str(n):
				add += int(s) ** 2
			n = add
			if add in list(record.keys()):
				return False
			else:
				record[add] = 1
		return True


class Solution:

	def isHappy(self, n):
		seen = {}
		while True:
			if str(n) in seen:
				return n == 1
			seen[str(n)] = True
			newN = 0
			while n > 0:
				(n, mod) = divmod(n, 10)
				newN += mod ** 2
			n = newN
		return False


class Solution:

	def isHappy(self, n):
		seen = set()
		while n not in seen:
			seen.add(n)
			n = sum([int(d) ** 2 for d in str(n)])
		return n == 1


class Solution:

	def isHappy(self, n):
		(slow, fast) = (n, n)
		while True:
			slow = self.getSquareSum(slow)
			fast = self.getSquareSum(fast)
			fast = self.getSquareSum(fast)
			if slow == fast:
				break
		if slow == 1:
			return True
		return False

	def getSquareSum(self, n):
		return sum([int(i) ** 2 for i in str(n)])


class Solution:

	def isHappy(self, n):
		sol = set()
		while n not in sol:
			sol.add(n)
			n = sum([int(i) ** 2 for i in str(n)])
		return n == 1


class Solution:

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


class Solution:

	def isHappy(self, n):

		def isHappyHelper(n):
			squaresum = 0
			while n:
				(n, remainder) = divmod(n, 10)
				squaresum += remainder ** 2
			if squaresum == 1:
				return True
			elif squaresum in seenumbers:
				return False
			else:
				seenumbers.add(squaresum)
				return isHappyHelper(squaresum)
		if n <= 0:
			return False
		seenumbers = set()
		seenumbers.add(n)
		return isHappyHelper(n)


class Solution(object):

	def isHappy(self, n):
		aset = set()
		while n != 1 and n not in aset:
			aset.add(n)
			sums = 0
			while n:
				digit = n % 10
				sums += digit * digit
				n = n // 10
			n = sums
		return n == 1


class Solution:

	def isHappy(self, n):
		count = {}
		while True:
			new_n = 0
			while n > 0:
				new_n = new_n + (n % 10) ** 2
				n = n // 10
			n = new_n
			if n == 1:
				return True
			if n in count:
				return False
			else:
				count[n] = 1