PIXELBANKv9.1.0
Menu

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 locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Happy Number - Easy | PixelBank