Solution: Happy Number

Statement

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

We use the following process to check if a given number is a happy number:

  • Starting with the given number numnum, replace the number with the sum of the squares of its digits.
  • Repeat the process until:
    • The number equals 11, which will depict that the given number numnum is a happy number.
    • It enters a cycle, which will depict that the given number numnum is not a happy number.

Return TRUE if numnum is a happy number, and FALSE if not.

Constraints

  • 1≤1 \leq num ≤231−1\leq 2^{31} - 1

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.