Happy Number

AtCoder
IDabc439_b
Time2000ms
Memory256MB
Difficulty
You are given a positive integer $N$. Determine whether $N$ is a happy number. A happy number is a non-negative integer that becomes $1$ after repeating the following operation a finite number of times: * Replace it with the integer obtained by taking the sum of the squares of the digits in its decimal representation. * For example, performing this operation once on $2026$ replaces it with $2^2+0^2+2^2+6^2 = 4+0+4+36 = 44$. For examples of happy numbers, refer to the explanations of sample inputs and outputs. ## Constraints * $N$ is an integer between $1$ and $2026$, inclusive. ## Input The input is given from Standard Input in the following format: $N$ [samples]
Samples
Input #1
2026
Output #1
Yes

$2026$ is a happy number.

*   The digits of $2026$ in decimal representation are $2,0,2,6$, and taking the sum of their squares gives $2^2+0^2+2^2+6^2 = 4+0+4+36 = 44$.
*   The digits of $44$ in decimal representation are $4,4$, and taking the sum of their squares gives $4^2+4^2 = 16+16 = 32$.
*   The digits of $32$ in decimal representation are $3,2$, and taking the sum of their squares gives $3^2+2^2 = 9+4 = 13$.
*   The digits of $13$ in decimal representation are $1,3$, and taking the sum of their squares gives $1^2+3^2 = 1+9 = 10$.
*   The digits of $10$ in decimal representation are $1,0$, and taking the sum of their squares gives $1^2+0^2 = 1+0 = 1$.

$2026$ is a happy number because it became $1$ after repeating the operation of replacing itself with the integer obtained by taking the sum of the squares of the digits in its decimal representation five times.
Input #2
439
Output #2
No

$439$ is not a happy number.
Repeating the operation makes it $439 \rightarrow 106 \rightarrow 37 \rightarrow 58 \rightarrow 89 \rightarrow 145 \rightarrow 42 \rightarrow 20 \rightarrow 4 \rightarrow 16 \rightarrow 37 \rightarrow \dotsb$, and it can be proved that no matter how many times the operation is repeated from here, it will not become $1$.
Input #3
440
Output #3
Yes

$440$ is a happy number.
API Response (JSON)
{
  "problem": {
    "name": "Happy Number",
    "description": {
      "content": "You are given a positive integer $N$. Determine whether $N$ is a happy number. A happy number is a non-negative integer that becomes $1$ after repeating the following operation a finite number of time",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc439_b"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a positive integer $N$. Determine whether $N$ is a happy number.\nA happy number is a non-negative integer that becomes $1$ after repeating the following operation a finite number of time...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments