[Community Puzzle] Happy Numbers


#1

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.


#2

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?


#4

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.


#5

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.


#6

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 + " :(");
            }
        }}

#7

Look carefully at the output of “Serious” test.

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


#8

I’m not exactly sure what that means not parsing it from string.


#9

@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)?


#10

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


#11

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


#12

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.


#13

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?


#14

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?


#15

don’t read input as integer.
read it as string.


#16

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


#17

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


#18

There is a mistake when i’m checking 761 it requires ‘happy’ but actually 761 isn’t happy


#19

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…


#20

I’ve already solved it
anyway thanks