#C12393. Check if a Number is Happy
Check if a Number is Happy
Check if a Number is Happy
You are given a positive integer ( n ). A number is called a ( \textbf{Happy Number} ) if by repeatedly replacing ( n ) with the sum of the squares of its digits, the process eventually reaches ( 1 ); otherwise, it will enter a cycle that does not include ( 1 ).
Formally, let $$F(n)=\sum_{d \in digits(n)} d^2.$$ Then, if after a finite number of iterations the value becomes ( 1 ), the number is happy. Otherwise, it is not a happy number.
For example, when ( n = 19 ):
$$19 \to 1^2+9^2 = 1+81=82$$
$$82 \to 8^2+2^2 = 64+4=68$$
$$68 \to 6^2+8^2 = 36+64=100$$
$$100 \to 1^2+0^2+0^2 = 1$$
Since the process ends in ( 1 ), ( 19 ) is a happy number.
inputFormat
The input consists of a single positive integer ( n ) (( 1 \leq n \leq 10^9 )) provided via standard input (stdin).
outputFormat
Output ( True ) if ( n ) is a happy number, otherwise output ( False ). The output should be printed to standard output (stdout).## sample
19
True