+ 1
How can I make this more efficient?
3 Answers
+ 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.
+ 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?
+ 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