September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -3,9 +3,11 @@ 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
|
||||
where
|
||||
p _ 1 = True
|
||||
p s n
|
||||
| n `member` s = False
|
||||
| otherwise = p (insert n s) (f n)
|
||||
f = sum . (((^ 2) . toInteger . digitToInt) <$>) . show
|
||||
|
||||
main = mapM_ print $ take 8 $ filter isHappy [1..]
|
||||
main = mapM_ print $ take 8 $ filter isHappy [1 ..]
|
||||
|
|
|
|||
|
|
@ -1,10 +1,18 @@
|
|||
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
|
||||
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 : (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..]
|
||||
main = print $ sum $ take 10000 $ filter happy [1 ..]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue