A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
11
Task/Happy-numbers/Haskell/happy-numbers-1.hs
Normal file
11
Task/Happy-numbers/Haskell/happy-numbers-1.hs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import Data.Char (digitToInt)
|
||||
import Data.Set (member, insert, empty)
|
||||
|
||||
isHappy :: Integer -> Bool
|
||||
isHappy = p empty
|
||||
where p _ 1 = True
|
||||
p s n | n `member` s = False
|
||||
| otherwise = p (insert n s) (f n)
|
||||
f = sum . map ((^2) . toInteger . digitToInt) . show
|
||||
|
||||
main = mapM_ print $ take 8 $ filter isHappy [1..]
|
||||
10
Task/Happy-numbers/Haskell/happy-numbers-2.hs
Normal file
10
Task/Happy-numbers/Haskell/happy-numbers-2.hs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import Data.Array
|
||||
|
||||
happy x = if xx <= 150 then seen!xx else happy xx where
|
||||
xx = dsum x
|
||||
seen :: Array Int Bool
|
||||
seen = listArray (1,150) $ True:False:False:False:(map happy [5..150])
|
||||
dsum n | n < 10 = n * n
|
||||
| otherwise = let (q,r) = n `divMod` 10 in r*r + dsum q
|
||||
|
||||
main = print $ sum $ take 10000 $ filter happy [1..]
|
||||
Loading…
Add table
Add a link
Reference in a new issue