[Community Puzzle] Happy Numbers

https://www.codingame.com/training/easy/happy-numbers

Send your feedback or ask for help here!

Created by @java_coffee_cup,validated by @Niako,@bbb000bbbyyy and @Deltaspace.
If you have any issues, feel free to ping them.

1 Like

I am using a while loop to repeat the process of taking the sum of the squares. Does anyone has any tips for me to check when the while loop is becoming infinite?

My own idea was to check the time it takes for running the application and if that exceeds an arbitrary value. I use Date.now() for that. This works for the first two test cases, but not for the larger numbers of the other test cases. Anyone has better ideas?

You can calculate first 20 iterations of algorithm for value 24 ( 20, 4, 16, ā€¦ ). Then look at this sequence and may be you see how to check it.

You can store numbers in data container like set, which stores only unique values. And then, just check if the set already contains current value.

1 Like

Iā€™m able to pass the first 2 levels but as soon as I get to serious it gives me an error. Iā€™m not sure what causes it. Hereā€™s my code if that helps. New to coding so its far from optimized I assume.

var num = [];
const N = parseInt(readline());
for (let i = 0; i < N; i++) {
    num.push(parseInt(readline()));
}
    for (i = 0; i < num.length; i++){
        newNum = num[i];
        for (y = 0; y < 100; y++){
            newNum = newNum.toString().split('').map(Number).map(n => n**2).reduce((a,b) => (a + b), 0);
        if (newNum === 1) { 
            answer = num[i].toString()
            console.log(answer + ' :)');
            break;}
        else if (y == 99) {
            answer2 = num[i].toString();
            console.log(answer2 + " :(");
            }
        }}

Look carefully at the output of ā€œSeriousā€ test.

You can try to work with really big numbers without parsing it from string (if JS).

Iā€™m not exactly sure what that means not parsing it from string.

@jPax101 JS numbers are 64-bit floating-point numbers (or more simply doubles) that cannot represent (without a loss of precision) the big integers given as input in this problem (doubles have 52 bits to store the digits of a positive number while the given integers ~10^26 would require 87 bits).
Hence you should not call parseInt on the input.
But do you really need it (as you call toString on them right after)?

1 Like

Thank you, removing the parseInt fixed the issue. Iā€™m not exactly sure what you meant by doubles and bits and all that. Iā€™m just starting my computer science degree. Where would I find info on that. Because tried this in python also and the same issues came up. I seem to get bad results at the last 2 levels.
none the less thanks for the support.
cheers mate

The sequence can stop if the result is either 1 or 4. If 1, the number is happy, otherwise unhappy.

Thank you. Indeed, I also found that on wikipedia, the unhappy eight-number cycle. However, I tried that and I still run into fails from serious onward.

If you fail only the bigger problem sets, it could be a time out. Your solution is too slow.
Are you still using the timestamp to determine when to stop the loop?

1 Like

I do not use time stamp anymore. I check if the 4 is popping up in the process.

I found something strange, which makes me fail the test cases for the bigger numbers. I work in Javascript.
If I do console.error(16525534153749833) it returns 16525534153749832.
When I do console.error(2712939616709395196), it returns 2712939616709395000.
When I do console.error(9244681613480703), it returns 9244681613480704.
With different inputs I get different expected outputs, which makes me unable to pass the later test cases. Anyone has an idea how to fix this issue?

donā€™t read input as integer.
read it as string.

2 Likes

@Wonderbaarlijke-Woud See also my previous post in this forum, which already explains that issue.

1 Like

Great post! I did not understood that this was my issue, but now I see it is. Thank you.

There is a mistake when iā€™m checking 761 it requires ā€˜happyā€™ but actually 761 isnā€™t happy

As you can see here (and in particular in this listing), 761 is the 107th happy number (761 -> 86 -> 100 -> 1).
Such a blatant mistake in a problem already solved by 855 people is very unlikelyā€¦

2 Likes

Iā€™ve already solved it
anyway thanks

I use: try and except RecursionError to test for infinite loop.