All tasks
Mediumhashmathloops

Happy Number

Time limit 2 s · Memory 64 MBAcceptance: —Solved by: 0

Statement

You are given an integer n. At every step replace the number with the sum of the squares of its digits. If the process reaches 1, the number is happy — print YES; if it enters a cycle and never reaches 1, print NO. For example: 19 → 82 → 68 → 100 → 1, so YES.

Examples

Example 1
Input
19
Output
YES
Example 2
Input
2
Output
NO

Constraints

  • 1 ≤ n ≤ 10^9
Solve this task to unlock the editorial.

AI tutor

Stuck? Get a nudge — the tutor explains the idea, it never hands you the solution.