#202
Happy Number
easy· Math & Geometryruns: 0Write an algorithm to determine if a number n is happy. A happy number is defined as follows: starting with any positive integer, replace the number by the sum of the squares of its digits and repeat. The number is happy if it eventually reaches 1; otherwise it loops endlessly in a cycle that does not include 1. Return true if n is happy, otherwise false.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 211
class Solution:
def isHappy(self, n: int) -> bool:
seen = set()
while n != 1 and n not in seen:
seen.add(n)
n = sum(int(d) ** 2 for d in str(n))
return n == 1
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.