RosettaCodeData/Task/Happy-numbers/APL/happy-numbers-2.apl
2023-07-01 13:44:08 -04:00

14 lines
737 B
APL
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

HappyNumbers{ ⍝ return the first ⍵ Happy Numbers
⍝ initial list
=+/: ⍝ 1's mark happy numbers
sq× ⍝ square function (times selfie)
isHappy{ ⍝ is ⍵ a happy number?
⍝ previous sums
=1:1 ⍝ if we get to 1, it's happy
n+/sq¨ ⍝ sum of the square of the digits
n:0 ⍝ if we hit this sum before, it's not happy
(,n) n} ⍝ recurse until it's happy or not
(,isHappy 1+) ⍝ recurse until we have ⍵ happy numbers
}
HappyNumbers 8
1 7 10 13 19 23 28 31