From 7594cb142212e0acf27157dd1cf90823144f432c Mon Sep 17 00:00:00 2001 From: Bill Ewanick Date: Wed, 27 Sep 2023 16:39:46 -0400 Subject: [PATCH] Finish 14 --- src/projectEuler/question14.hs | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/projectEuler/question14.hs b/src/projectEuler/question14.hs index c8a4a8e..34cf393 100644 --- a/src/projectEuler/question14.hs +++ b/src/projectEuler/question14.hs @@ -16,33 +16,23 @@ 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 = head $ head $ sortOn (Down . length) $ map collatzChainStartingAt [1..limit] +nextCollatz :: Integer -> Integer nextCollatz n | n == 1 = 0 | even n = n `div` 2 | odd n = 3*n + 1 +collatzChainStartingAt :: Integer -> [Integer] collatzChainStartingAt n = takeWhile (/= 0) $ iterate nextCollatz n --- solve limit = (n, chain) --- where --- s = maximum --- $ map length --- $ map collatzChainStartingAt [1..limit] +limit :: Integer limit = 1_000_000 - -wholePositiveNumbers :: IntSet -wholePositiveNumbers = IntSet.fromDistinctAscList [1..limit] - -f = IntSet.foldl (\acc key -> collatzChainStartingAt key : acc) []