For a natural number n, define the function f as follows.
f(n) is the sum of the squares of the digits of n written in base ten.
For example, if n=19, then f(19)=82 because 12+92=82.
Applying f over and over, some natural numbers eventually reach 1. Such a number is called a happy number. 19 is one of them.
- f(19)=12+92=82
- f(82)=82+22=68
- f(68)=62+82=100
- f(100)=12+02+02=1
Not every natural number is happy. Work 5 out by hand and you will see that 5 is not a happy number. Mathematicians proved that when n is not a happy number, repeated application of f always falls into this cycle.
4→16→37→58→89→145→42→20→4
Write a program that decides whether a given natural number n is a happy number.