PIXELBANKv8.2.1
Menu

Happy Number

A happy number is defined by repeatedly replacing it with the sum of squares of its digits until it either reaches 1 (happy) or loops endlessly (not happy).

Return True if n is a happy number.

Example:

Input:
19
Output:
True
Reasoning:
  • We start with the input number 1919 and calculate the sum of squares of its digits: 12+92=1+81=821^2 + 9^2 = 1 + 81 = 82
  • Then, we repeat the process with 8282: 82+22=64+4=688^2 + 2^2 = 64 + 4 = 68
  • Next, we calculate the sum of squares of the digits of 6868: 62+82=36+64=1006^2 + 8^2 = 36 + 64 = 100
  • Finally, we continue this process: 12+02+02=11^2 + 0^2 + 0^2 = 1, which is 11, so the number 1919 is happy, and we return TrueTrue

Constraints:

  • 1 <= n <= 2^31 - 1
Editor

Test Results

0/0
Run code to see test results.