➜ ~

Playing Hacks and Stuffs!


Project maintained by h4ckyou Hosted on GitHub Pages — Theme by mattgraham

Happy Number

image

Write an algorithm to determine if a number n is happy.

A happy number is a number defined by the following process:

Return true if n is a happy number, and false if not.

Solve Script: link image

Leetcode Submission Script

class Solution:
    def isHappy(self, n: int) -> bool:
        n=str(n)
        ch=False
        if n=='1':
            ch=True
        else:
            while n not in ('2', '3', '4', '5', '6', '8','9'):
                print(n,1)
                s=0
                for i in n:
                    s+=int(i)**2
                if s==1:
                    ch=True
                    break
                else:
                    n=str(s)
        return ch