On Sep 14, 11:23 am, neeraj mehta <[email protected]> wrote:
> Can somebody explain me how to check wether a number is HAPPY or not for a
> given base...
>

Let N_d be Decimal representation of a number N_b (in base 'b').
For example, N_b = 101 (b=2), N_d = 5.

Ok, be convinced that whatever base are you using to perform
mathematical operations between numbers. Their resultant value remains
the same. Representation may differ though, for different bases. For
example, in base 10, 5 * 2 = 10. Likely, in base 2, 101 * 10 = 1010.
You may not know how to multiply in base 2, but you do know how to do
that in base 10. So they key is, perform the squaring of digits and
summing in base 10. Convert the result back to required base. Again,
convert each digit of the base representation of the result to decimal
and square and add them again. And follow this cycle..

Note: Since it is given that all bases <= 10. There is no need to
convert individual digits of a base representation to decimal. They
will yield same value. For instance, digit 5 in base 7 will remain 5
in base 10 also.

pseudo code:

isHappy(N_d, b): -> bool
    do-while:
         if (N_d == 1) return true;
         Mark N_d as visited
         N_d = getNext(N_d, b)
    end-while (N_d not visited before);
    return false;
end of isHappy()

getNext(N_d, b): -> int
    N_b <- Convert N_d in base b
    res <- Sum of squares of the digits in N_b
    return res;
end of getNext()
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-codejam" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-code?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to