All problems

Problem 89

Happy Number

Easy · 10 pts · Math, Hash Table

Problem

Start with a positive integer N and repeatedly replace it with the sum of the squares of its digits. If the process eventually reaches 1, N is a happy number. If it never reaches 1, it gets stuck in a cycle that repeats forever, and N is not happy. Decide whether N is happy. Keep a set of the numbers you have already seen and stop as soon as a number repeats (not happy) or you reach 1 (happy). Floyd's slow and fast pointer technique also works without the set.

Input

A single integer N.

Output

Print YES or NO.

Constraints

1 <= N <= 2147483647

Example input

19

Example output

YES

Explanation

From 19: 1² + 9² = 82, then 8² + 2² = 68, then 6² + 8² = 100, then 1² + 0² + 0² = 1. The process reaches 1, so 19 is happy: YES.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Binomial Coefficient Modulo Next: Integer Square Root