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.

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.

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.

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).

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

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?