Sunday, December 29, 2013

SPOJ 7733. Happy Numbers I

Here is the problem statement
http://www.spoj.com/problems/HPYNOS/

This task is a task of implementation with a little bit of care. We can always calculate the sum of the squares of digits of a number. We will keep doing this until our number reaches 1 or it reaches a number which we already processed before . For keeping track of numbers we already processed we will keep a boolean array called mark and we will mark the indexes of this array as true if we process a number. Example
we are on number N. we write mark[N]=true   And we constantly check if mark[N] is true (AKA we have already processed it) that means that thegiven number is ont "happy number". We don't need to keep a boolean array of 2,147,483,647  because of the following. There are 10 digits in this number. Even if all of them were 9s (maximum value) the second number was going to be 81*10=810 so tha mark array of size 900 is completely enough.

       DOWNLOAD THE FULL SOURCE CODE

No comments:

Post a Comment