happy_number
1# @leet start 2class Solution: 3 def isHappy(self, n: int) -> bool: 4 """ 5 This question asks to return if `n` is a happy number, which either 6 ends at 1 or loops through the process where it is replaced by the sum 7 of the square of its digits. 8 9 To find the square of the digits of a number, we can do the following: 10 ```py 11 total = 0 12 while n > 0: 13 n, digit = divmod(n, 10) 14 total += digit ** 2 15 ``` 16 where `total` is the new number. 17 We then set the conditions, where looping returns False and if n == 1 18 then we return true. 19 20 Finally we recurse through, since we know there's either a loop or 21 the number ends at 1. 22 """ 23 visited = set() 24 25 def num_to_sum(n): 26 if n == 1: 27 return True 28 if n in visited: 29 return False 30 visited.add(n) 31 total = 0 32 while n > 0: 33 n, digit = divmod(n, 10) 34 total += digit**2 35 return num_to_sum(total) 36 37 return num_to_sum(n) 38 39 40# @leet end 41 42 43def test(): 44 assert 2 + 2 == 4
class
Solution:
3class Solution: 4 def isHappy(self, n: int) -> bool: 5 """ 6 This question asks to return if `n` is a happy number, which either 7 ends at 1 or loops through the process where it is replaced by the sum 8 of the square of its digits. 9 10 To find the square of the digits of a number, we can do the following: 11 ```py 12 total = 0 13 while n > 0: 14 n, digit = divmod(n, 10) 15 total += digit ** 2 16 ``` 17 where `total` is the new number. 18 We then set the conditions, where looping returns False and if n == 1 19 then we return true. 20 21 Finally we recurse through, since we know there's either a loop or 22 the number ends at 1. 23 """ 24 visited = set() 25 26 def num_to_sum(n): 27 if n == 1: 28 return True 29 if n in visited: 30 return False 31 visited.add(n) 32 total = 0 33 while n > 0: 34 n, digit = divmod(n, 10) 35 total += digit**2 36 return num_to_sum(total) 37 38 return num_to_sum(n)
def
isHappy(self, n: int) -> bool:
4 def isHappy(self, n: int) -> bool: 5 """ 6 This question asks to return if `n` is a happy number, which either 7 ends at 1 or loops through the process where it is replaced by the sum 8 of the square of its digits. 9 10 To find the square of the digits of a number, we can do the following: 11 ```py 12 total = 0 13 while n > 0: 14 n, digit = divmod(n, 10) 15 total += digit ** 2 16 ``` 17 where `total` is the new number. 18 We then set the conditions, where looping returns False and if n == 1 19 then we return true. 20 21 Finally we recurse through, since we know there's either a loop or 22 the number ends at 1. 23 """ 24 visited = set() 25 26 def num_to_sum(n): 27 if n == 1: 28 return True 29 if n in visited: 30 return False 31 visited.add(n) 32 total = 0 33 while n > 0: 34 n, digit = divmod(n, 10) 35 total += digit**2 36 return num_to_sum(total) 37 38 return num_to_sum(n)
This question asks to return if n is a happy number, which either
ends at 1 or loops through the process where it is replaced by the sum
of the square of its digits.
To find the square of the digits of a number, we can do the following:
total = 0
while n > 0:
n, digit = divmod(n, 10)
total += digit ** 2
where total is the new number.
We then set the conditions, where looping returns False and if n == 1
then we return true.
Finally we recurse through, since we know there's either a loop or the number ends at 1.
def
test():