1
0
Fork 0

Even wilder performance increases

main
Bill Ewanick 2023-09-27 17:04:20 -04:00
parent bc28995115
commit d4cf6b25c8
1 changed files with 6 additions and 6 deletions

View File

@ -16,24 +16,24 @@ NOTE: Once the chain starts the terms are allowed to go above one million.
-} -}
module Main where module Main where
import Data.List (genericLength, sortOn) import Data.List (sortOn)
import Data.Ord (Down (Down)) import Data.Ord (Down (Down))
main :: IO () main :: IO ()
main = print ans main = print ans
ans :: Integer ans :: Int
ans = fst $ head $ sortOn (Down . snd) $ map collatzChainStartingAt [1..limit] ans = fst $ head $ sortOn (Down . snd) $ map collatzChainStartingAt [1..limit]
nextCollatz :: Integer -> Integer nextCollatz :: Int -> Int
nextCollatz n nextCollatz n
| n == 1 = 0 | n == 1 = 0
| even n = n `div` 2 | even n = n `div` 2
| odd n = 3*n + 1 | odd n = 3*n + 1
collatzChainStartingAt :: Integer -> (Integer, Integer) collatzChainStartingAt :: Int -> (Int, Int)
collatzChainStartingAt n = (n, s') 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 limit = 1_000_000