These challenge posts have been a lot of fun. Another one inspired by Fermat's Library.
Challenge
In the language of your choice, write a script to find "Happy Numbers." Via Wikipedia:
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits in base-ten, and repeat the process until the number either equals 1 (where it will stay), or it loops endlessly in a cycle that does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers).
Looking at the picture in the embedded tweet will probably help solidify the process. Here are the first few happy numbers to help check your work:
1, 7, 10, 13, 19, 23, 28, 31
Have fun!
Top comments (28)
Here is a nice solution in JavaScript:
Then you can play with generators for instance:
Another quick PHP one. Betting there is a more performant method but will leave that to someone else. :)
How does this look? I initially had happy memoizing previous numbers, but it seemed like it ran fast enough without it, and the memoizing cluttered things up.
Also, I didn't know about the digits method until now! :)
e: updated to work with all happy numbers
Your code in ES2016+ (and a bit less readable):
You could even shorten the
toString
by replacing it with a template string which will do an implicittoString
.Looking at your solution it actually fails later in the mix. I was pretty sure you couldn't rely on the numbers not adding up to a single digit happy number (which the only other one beyond 1 is 7.)
1111111 is a happy number which will become 7 after the first iteration and 7 is a happy number. This fails your current function. Here is an updated one that would work by testing if these ever hit 4 instead of length of 1.
Hmmm, good catch :D Although the fact you found all the sad numbers end in 4 is even more interesting to me.🤔🤔
I read the wikipedia article cause I was like "wait if it's not happy wouldn't that go in to an infinite loop?" :)
The number 42 is also in that loop, by the way. :)
Adding a solution in Haskell:
Note that Haskell function composition with the (.) operator is read from right to left (or from bottom to top, respectively). The hardest part for FP is folding with a non-trivial break condition, here.
And now that I have learned a simpler break condition for the problem, it is easier:
On the other hand, I like the possibility of my original code to see the list of intermediate results to get a feeling for the problem.
A work in two parts. First, Happy.pm, a Perl5 Module.
Which is called by a program.
Not particularly golfy, because I like reading and understanding things.
Here's mine in C#. I feel like this could have been way shorter though. Oh well :p
bro log code is not good......
Thanks for letting me know.
What should I change so that my code improves?
your feedback...
your code write is good and logic is also good , but you code very long ,,,
so, listen ,try to write code small, simpal
Here is my C# implementation:
Here's my previously golfed version in perl6 made slightly more readable. Includes a state based cache to reduce recursion. The final line prints the happy numbers up to 200 (well up to the last befor 200)
Elm solution which does not use strings. Hopefully quite understandable.
Two recursive functions. Exit conditions are getting a number between 0 and 9. Zero is impossible to actually get from other numbers, but there if someone input 0. The only happy numbers in that range are 1 and 7. Should work with negative numbers too.
Here is a runnable solution with UI to test a range of numbers. Try it at elm-lang.org/try.