From d4cf6b25c85f6bd17b24f769be147a2df462f6cd Mon Sep 17 00:00:00 2001 From: Bill Ewanick Date: Wed, 27 Sep 2023 17:04:20 -0400 Subject: [PATCH] Even wilder performance increases --- src/projectEuler/question14.hs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/projectEuler/question14.hs b/src/projectEuler/question14.hs index a4128ed..13a649b 100644 --- a/src/projectEuler/question14.hs +++ b/src/projectEuler/question14.hs @@ -16,24 +16,24 @@ NOTE: Once the chain starts the terms are allowed to go above one million. -} module Main where -import Data.List (genericLength, sortOn) +import Data.List (sortOn) import Data.Ord (Down (Down)) main :: IO () main = print ans -ans :: Integer +ans :: Int ans = fst $ head $ sortOn (Down . snd) $ map collatzChainStartingAt [1..limit] -nextCollatz :: Integer -> Integer +nextCollatz :: Int -> Int nextCollatz n | n == 1 = 0 | even n = n `div` 2 | odd n = 3*n + 1 -collatzChainStartingAt :: Integer -> (Integer, Integer) +collatzChainStartingAt :: Int -> (Int, Int) collatzChainStartingAt n = (n, s') - where s' = genericLength $ takeWhile (/= 0) $ iterate nextCollatz n + where s' = length $ takeWhile (/= 0) $ iterate nextCollatz n -limit :: Integer +limit :: Int limit = 1_000_000