Algorithm of 10591 - Happy Number

Problem Description Link
Algorithm:
Iterating this sum-of-squared-digits map always eventually
 reaches one of the 10 numbers 0, 1, 4, 16, 20, 37, 42, 58, 89, or 145
Examples:
4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4.
This sequence starts with 4 and 4 occurs again later, a periodic sequence. => 4 unhappy.

17 -> 50 -> 25 -> 29 -> 85 -> 89 -> 145 -> 42 -> 20 -> 4 -> 16 -> 37 -> 58 -> 89.
This sequence starts with 17, but there is periodic sub-sequence inside. => 17 unhappy.
7 is a Happy number since 7 -> 49 -> 97 -> 130 -> 10 -> 1 .
Or
for unhappy number you can check  sum-of-squared-digits that all ready get that value repeat
i.e. for number 17 you see 89 repeat so it is a unhappy number.
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience