# [Community Puzzle] Happy Numbers

Just for linkage between near topics : as i said in this topic (Groovy gets timeouts in every clash now - #17 by Jp82), Groovy works now for me in both IDE and Validators …
=> so, i think you may have solved the issue
Thanks a lot to anyone involved !

1 Like

It was a great advice. I was stuck in this problem for 3 hrs. Thank You

Hi @TanmaySharan : so, now you are Happy like a number ?

Hello there,

I have been working with this problem for awhile in C# and cannot for some reason see why my code is not working. Could someone please help?

Here is my code:

static bool HappyCheck(int n){
bool isHappy = false;
int sum = 0;

while(n > 0){
sum += (n % 10) * (n % 10);
n /= 10;
}

if(sum == 1){
isHappy = true;
}
else if(sum == 4){
isHappy = false;
}
return isHappy;
}

static void Main(string[] args)
{
int num = 0;

for (int i = 0; i < N; i++)
{
string happy = "";

try{
num = int.Parse(x);
if(HappyCheck(num)){
happy = ":)";
}
else{
happy = ":(";
}
}

catch(OverflowException){
if(HappyCheck(num)){
happy = ":)";
}
else{
happy = ":(";
}
}
Console.WriteLine(x + " " + happy);
}
}

You have to

repeat the process until the number either equals 1 (where it will stay), or it loops endlessly in a cycle that does not include 1

as stated in the puzzle statement.

1 Like

Could you please explain more because I thought I was already doing this with the while loop in my HappyCheck(int n) function?

The requirement is:

while (conditionA OR conditionB) {
repeat the process, which will update the input for the 2 conditions,
so that every time you will have new conditions to test
}

But your while () does not have the two conditions tested. You test it after the while loop and it does not affect the 2 conditions.

1 Like

So what you’re saying is in my while loop, I need to test both conditions. Thank you for helping me understand the problem. I’m thinking of doing something like this:

while(num > 0 && (sum != 1 || sum != 4)){
remainder = num % 10;
sum += remainder * remainder;
num /= 10;
}

it looks more logical than before

I’ve had a small breakthrough with my code!

int cycle = N * 5000;
for (int i = 0; i < N; i++)
{
string happy = "";
bool converted = Int32.TryParse(x, out num);

if(converted){
while(cycle > 0 && num != 1){
num = HappyCheck(num);
cycle--;
if(cycle == 0 && num != 1){
happy = ":(";
}
}

if(num == 1){
happy = ":)";
}
cycle = N * 5000;
}
Console.WriteLine(x + " " + happy);
}

And while this works for the first two cases, it does not for the last three. What am I doing wrong?

I just changed the variables I declared as integers in my code to long and now the third testcase works. But the other two don’t. Anybody have an idea on how to resolve this issue?

The puzzle has warned you, your data type cannot handle the very long and big numbers.
Do not transform the super big number into your super big type.

Check the Example test case, input is 23.
Do you really need using 23 in your calculation?
I don’t think so. You start calculating with 2 and 3, and then deal with some other small numbers only.

1 Like

Check if result are looping.

Hello,

I can not pass the 2nd test.

### Failure @ “moderate” test

1121
1121

is 1121 happy or unhappy ?

Hi,

1121 is an happy number.
1121 => 7 => 49 => 97 => 130 => 10 => 1

I thought that with only one digit left, the sum of square stopped. I solved the problem now, thanks !

This made me dig deep into a lot of long lost C [redacted] logic that I had nearly forgotten.
It would have been much simpler in something like rust with native 128-bit support, but this was a fun challenge in C.