is_happy.py 309 B

12345678910111213141516
  1. import math
  2. def isHappy(n: int) -> bool:
  3. if n == 1:
  4. return True
  5. while n != 1:
  6. n = str(n)
  7. a = 0
  8. for i in n:
  9. i = int(i)
  10. a = i * i + a
  11. if a < 10 and a > 1 and a != 7:
  12. return False
  13. n = a
  14. return True
  15. print(isHappy(2))