re: Write a script to find "Happy Numbers" VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Adding a solution in Haskell: -- core function; read from bottom to top f :: Int -> Int f = sum -- Sum up the squares . map ((^2)...
 

And now that I have learned a simpler break condition for the problem, it is easier:

f :: Int -> Int
f = sum          -- Sum up the squares
  . map ((^2)    -- Compute squares
         .read   -- Convert digits to Ints
         .(:[])) -- Explode number string to list of digit strings
  . show         -- Convert input number to string

isHappy :: Int -> Bool
isHappy n | n == 4 = False
          | n == 1 = True
          | otherwise = isHappy (f n)

main = print $ filter isHappy [1..200]

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.

code of conduct - report abuse