[Community Puzzle] Happy Numbers

Input:

50
7
8
86
10
32
10
310
913
108
3925
57714
204650
3935999
8869716
5310265
1305462
81915006
62187788
37434030
82893156
357558526
389679728
429968695
4614121900
3273949923
3402040497
8390063700
75860300187
3830929637583
7456574720517
76083067902031
43231406433477
84658586744469
566855069295730
293024126836742
6987862321189978
2481331054005573
8550506486727537
52611351637349120
633736527285728075
4280271021537705216
7923785328480252021
74692295997080392994
342639328902349085500
3635171293808307965209
4610726264688936129498
2411843019493615815308
89073551194714198974652
3547617484839572359410360
7547346898512036957709921

Output

7 :)
8 :(
86 :)
10 :)
32 :)
10 :)
310 :)
913 :)
108 :(
3925 :(
57714 :(
204650 :(
3935999 :)
8869716 :)
5310265 :)
1305462 :)
81915006 :)
62187788 :)
37434030 :(
82893156 :(
357558526 :)
389679728 :(
429968695 :(
4614121900 :(
3273949923 :(
3402040497 :(
8390063700 :(
75860300187 :(
3830929637583 :)
7456574720517 :(
76083067902031 :)
43231406433477 :)
84658586744469 :(
566855069295730 :)
293024126836742 :(
6987862321189978 :(
2481331054005573 :(
8550506486727537 :(
52611351637349120 :(
633736527285728075 :(
4280271021537705216 :(
7923785328480252021 :(
74692295997080392994 :(
342639328902349085500 :(
3635171293808307965209 :)
4610726264688936129498 :(
2411843019493615815308 :(
89073551194714198974652 :(
3547617484839572359410360 :(
7547346898512036957709921 :(

Thanks so much for posting validator 5, and I found a careless mistake.

Hello to All :slight_smile: !
Thanks to @CGTeam and @TwoSteps : Groovy seems to build again :grinning: !
Unfortunately, all my Tests pass in Groovy, but none of the Validators …
→ Is there a mistake somewhere in my code ?
Thanks to anyone who can help me :wink: !

Issue is not in your code. We still have problems with Groovy for submissions. We’ll take care of it.

1 Like

OK, Thanks @TwoSteps for your reply and all the @CGTeam too for your job :slight_smile: !
→ I’ll wait for later Groovy submissions :wink:
Have a good day :sunny: !

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 :astonished:
Thanks a lot to anyone involved :clap: !

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 :wink: ?

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 N = int.Parse(Console.ReadLine());
    int num = 0;

    for (int i = 0; i < N; i++)
    {
        string x = Console.ReadLine();
        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;
}

1 Like

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 x = Console.ReadLine();
            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 :frowning:
1121 :slight_smile:

is 1121 happy or unhappy ?

Hi,

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