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!
Latest comments (28)
Here's a golang implementation, optimized for speed. It takes about 200ms on my machine to find all the happy numbers from 1 to a million on my machine if you take out the prints.
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
Golang Solution
package main import ( "fmt" "strconv" "strings" ) func isHappyNumber(num int) bool { newNum := 0 switch num { case 1: return true case 4: return false default: s := strconv.Itoa(num) digits := strings.Split(s, "") for _, i := range digits { n, _ := strconv.Atoi(i) newNum += n * n } } return isHappyNumber(newNum) } func main() { fmt.Println(isHappyNumber(94)) }Can’t resist, here’s a solution in Reason: (Try it)
I'm glad to see some Haskell here. :) I somehow prefer your computational approach to split the number over my quick string hack. You can easily change the base.
I have done a lot of parsing stuff in Haskell with parsec, attoparsec and readP, lately. So I instantly think of parsing when I see the problem of splitting a number into digits. The detour via strings is comfortable,
because it uses the read parser.but"Challenge" accepted to solve it computationally. :)
To import as few / basic libraries as reasonably achievable, I decided not to use the parsec library but to write a homemade digit parser.
What you do in splitNum reminds me of the quotRem function (that I found when I coded for the Challenge about Kaprekar numbers, here). quotRem just needs a swap to make for a nice digit parser in a very organic way: When dividing by 10 the integer quotient is the state of the state monad (that's still to parse) and the remainder is a parsed digit.
I am a bit biased to use (faster) Ints in my prototypes, and use Integers where really needed, but I adopt your approach with Integers by at least generalizing the code to Integral types.
Although I learned to prefer folds over homemade recursion, I'm afraid the abstractness of foldP is a little bit hard to read, so perhaps I stick with the homemade recursion, this time. :)
F#
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 is my C# implementation:
The obvious pythonic way:
Jokes aside, here's a Python implementation using simple memoization:
A simple Rust Solution