Skip to content

Instantly share code, notes, and snippets.

@valyagolev
Created March 31, 2010 20:05
Show Gist options
  • Save valyagolev/350802 to your computer and use it in GitHub Desktop.
Save valyagolev/350802 to your computer and use it in GitHub Desktop.
import List ( elemIndex )
import Char ( digitToInt )
check ones nines [] = length nines
check ones nines (x:xs) = if (ending x) == 1 then check (x:ones) nines xs else 1 + check ones (x:nines) xs
where
ending 1 = 1
ending 89 = 89
ending x = case elemIndex x ones of
Just _ -> 1
Nothing -> case elemIndex x nines of
Just _ -> 89
Nothing -> ending' x
ending' x = ending $ (sum $ map (^2) $ map digitToInt $ show x)
checkCount xs = check [] [] xs
main = print $ checkCount [1..10000000]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment