1
0
Fork 0

Compare commits

..

3 Commits

Author SHA1 Message Date
Bill Ewanick d4cf6b25c8 Even wilder performance increases 2023-09-27 17:04:20 -04:00
Bill Ewanick bc28995115 Add performance improvement 2023-09-27 16:51:16 -04:00
Bill Ewanick 7594cb1422 Finish 14 2023-09-27 16:39:46 -04:00
1 changed files with 9 additions and 18 deletions

View File

@ -16,33 +16,24 @@ NOTE: Once the chain starts the terms are allowed to go above one million.
-}
module Main where
-- import Data.Set (Set)
-- import qualified Data.Set as Set
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
import Data.List (sortOn)
import Data.Ord (Down (Down))
main :: IO ()
main = print ans
ans :: Integer
ans = 42
ans :: Int
ans = fst $ head $ sortOn (Down . snd) $ map collatzChainStartingAt [1..limit]
nextCollatz :: Int -> Int
nextCollatz n
| n == 1 = 0
| even n = n `div` 2
| odd n = 3*n + 1
collatzChainStartingAt n = takeWhile (/= 0) $ iterate nextCollatz n
collatzChainStartingAt :: Int -> (Int, Int)
collatzChainStartingAt n = (n, s')
where s' = length $ takeWhile (/= 0) $ iterate nextCollatz n
-- solve limit = (n, chain)
-- where
-- s = maximum
-- $ map length
-- $ map collatzChainStartingAt [1..limit]
limit :: Int
limit = 1_000_000
wholePositiveNumbers :: IntSet
wholePositiveNumbers = IntSet.fromDistinctAscList [1..limit]
f = IntSet.foldl (\acc key -> collatzChainStartingAt key : acc) []