haskell: add [p12, p17]

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2022-11-07 19:17:35 +05:30
parent 22fdf20500
commit de6ea02b14
7 changed files with 185 additions and 5 deletions

15
haskell/p14.hs Normal file
View File

@@ -0,0 +1,15 @@
import Data.Ord (comparing)
import Data.List (maximumBy)
main :: IO ()
main = putStr $ show $ fst $ maximumBy (comparing snd) $ zip range $ map collatz range
where
range :: [ Int ]
range = [1 .. 10 ^ 6 - 1]
-- eh 12s isn't much
collatz :: Int -> Int
collatz n
| n == 1 = 1
| even n = 1 + collatz (div n 2)
| otherwise = 1 + collatz (3 * n + 1)