This post is part of a series on Mohammad Anwar’s excellent Weekly Challenge, where hackers submit solutions in Perl, Raku, or any other language, to two different challenges every week. (It’s a lot of fun, if you’re into that sort of thing.)
Task #2 this week comes to us from Robert DiCicco, and Robert demands happiness! Or at least numbers that are happy. Either way, I could use some right about now.
We are to find the first 8 Happy Numbers in base 10. To test whether a number is Happy:
replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
Weekly Challenge #164
Those numbers for which this process end in 1 are happy numbers, while those numbers that do not end in 1 are unhappy numbers.
If You’re Happy and You Know It return 1
Using the algorithm exactly as described above, the Perl code is straightforward:
Continue reading “PWC 164 › Happy Numbers”