Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
14
Task/Happy-numbers/Haskell/happy-numbers-1.hs
Normal file
14
Task/Happy-numbers/Haskell/happy-numbers-1.hs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
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 . fmap ((^ 2) . toInteger . digitToInt) . show
|
||||
|
||||
main :: IO ()
|
||||
main = mapM_ print $ take 8 $ filter isHappy [1 ..]
|
||||
19
Task/Happy-numbers/Haskell/happy-numbers-2.hs
Normal file
19
Task/Happy-numbers/Haskell/happy-numbers-2.hs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import Data.Array (Array, (!), listArray)
|
||||
|
||||
happy :: Int -> Bool
|
||||
happy x
|
||||
| xx <= 150 = seen ! xx
|
||||
| otherwise = happy xx
|
||||
where
|
||||
xx = dsum x
|
||||
seen :: Array Int Bool
|
||||
seen =
|
||||
listArray (1, 150) $ True : False : False : False : (happy <$> [5 .. 150])
|
||||
dsum n
|
||||
| n < 10 = n * n
|
||||
| otherwise =
|
||||
let (q, r) = n `divMod` 10
|
||||
in r * r + dsum q
|
||||
|
||||
main :: IO ()
|
||||
main = print $ sum $ take 10000 $ filter happy [1 ..]
|
||||
Loading…
Add table
Add a link
Reference in a new issue