Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Happy Number

XPChallenge Points: 10
levelLevel: Easy

Determine whether a number is joyful based on a repeated digit-squaring process.

Here’s how it works:

  • Begin with any positive integer.

  • Replace the number with the sum of the squares of its digits.

  • Keep repeating this process until the number becomes 1 (meaning it’s joyful) or it begins to cycle endlessly without reaching 1.

If the process ends in 1, the number is joyful; otherwise, it’s not.

Return true if the number is joyful, otherwise false.

Example 1:

Input: n = 7

Output: true

Explanation:

7² = 49 4² + 9² = 97 9² + 7² = 130 1² + 3² + 0² = 10 1² + 0² = 1 → Joyful!

Example 2:

Input: n = 4

Output: false

Explanation:

4² = 16 1² + 6² = 37 3² + 7² = 58 5² + 8² = 89 8² + 9² = 145 1² + 4² + 5² = 42 4² + 2² = 20 2² + 0² = 4 → Loops again → Not joyful

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result