14th Jun 2019, 10:09 PM
Daniel Cooper
3 Réponses
+ 1
Your check for a happy number does not work because it only runs a set number of times, not until it is determined to be a happy number or not. You should work on making your code function properly before worrying about efficiency.
14th Jun 2019, 11:11 PM
Jackson O’Donnell
+ 1
Jackson O’Donnell That works fine. I've tested it. What do you mean exactly? Edit: Just to be clear, the function runs until it reaches one, or it catches up to itself. Not a set number of times. Where did you get that from?
14th Jun 2019, 11:38 PM
Daniel Cooper
+ 1
I'm sorry, I didn't see the loop. A way to make it more efficient would be to make an array w/ all the digits squared and pull from that rather than using multiplication. int digSquares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] Then sum += digSquares[x%10]; You can also save known digit-sum pairs in a map and look through that for the sum first, but this will onky work with very large numbers
14th Jun 2019, 11:46 PM
Jackson O’Donnell