#C10120. Happy Number Check
Happy Number Check
Happy Number Check
Given a positive integer \( n \), determine whether it is a happy number. A number is called happy if, starting with \( n \), the process of replacing the number by the sum of the squares of its digits eventually leads to 1. Otherwise, the number is unhappy.
Mathematically, if \( n \) has digits \( d_1, d_2, \dots, d_k \), then the next number is computed as:
[ n = \sum_{i=1}^{k} d_i^2 ]
This process is repeated until the number becomes 1 (in which case it is happy, output "YES") or until it loops continuously in a cycle that does not include 1 (in which case it is not happy, output "NO").
For example, if \( n = 19 \):
[ 1^2 + 9^2 = 1 + 81 = 82 \rightarrow \dots \rightarrow 1 ]
Thus, 19 is a happy number.
inputFormat
The input consists of a single integer \( n \) provided via standard input.
outputFormat
Output a single line to standard output containing YES
if \( n \) is a happy number, or NO
otherwise.
19
YES