Finish 14
parent
e6e3cfbccc
commit
7594cb1422
|
@ -16,33 +16,23 @@ NOTE: Once the chain starts the terms are allowed to go above one million.
|
||||||
-}
|
-}
|
||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
-- import Data.Set (Set)
|
import Data.List (sortOn)
|
||||||
-- import qualified Data.Set as Set
|
import Data.Ord (Down (Down))
|
||||||
|
|
||||||
import Data.IntSet (IntSet)
|
|
||||||
import qualified Data.IntSet as IntSet
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = print ans
|
main = print ans
|
||||||
|
|
||||||
ans :: Integer
|
ans :: Integer
|
||||||
ans = 42
|
ans = head $ head $ sortOn (Down . length) $ map collatzChainStartingAt [1..limit]
|
||||||
|
|
||||||
|
nextCollatz :: Integer -> Integer
|
||||||
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]
|
||||||
collatzChainStartingAt n = takeWhile (/= 0) $ iterate nextCollatz n
|
collatzChainStartingAt n = takeWhile (/= 0) $ iterate nextCollatz n
|
||||||
|
|
||||||
-- solve limit = (n, chain)
|
limit :: Integer
|
||||||
-- where
|
|
||||||
-- s = maximum
|
|
||||||
-- $ map length
|
|
||||||
-- $ map collatzChainStartingAt [1..limit]
|
|
||||||
limit = 1_000_000
|
limit = 1_000_000
|
||||||
|
|
||||||
wholePositiveNumbers :: IntSet
|
|
||||||
wholePositiveNumbers = IntSet.fromDistinctAscList [1..limit]
|
|
||||||
|
|
||||||
f = IntSet.foldl (\acc key -> collatzChainStartingAt key : acc) []
|
|
||||||
|
|
Loading…
Reference in New Issue