Compare commits
	
		
			No commits in common. "d4cf6b25c85f6bd17b24f769be147a2df462f6cd" and "e6e3cfbccc8233d22dc7d99ee59f28b4ba571c2a" have entirely different histories. 
		
	
	
		
			d4cf6b25c8
			...
			e6e3cfbccc
		
	
		|  | @ -16,24 +16,33 @@ NOTE: Once the chain starts the terms are allowed to go above one million. | ||||||
| -} | -} | ||||||
| module Main where | module Main where | ||||||
| 
 | 
 | ||||||
| import           Data.List (sortOn) | -- import           Data.Set    (Set) | ||||||
| import           Data.Ord  (Down (Down)) | -- import qualified Data.Set    as Set | ||||||
|  | 
 | ||||||
|  | import           Data.IntSet (IntSet) | ||||||
|  | import qualified Data.IntSet as IntSet | ||||||
| 
 | 
 | ||||||
| main :: IO () | main :: IO () | ||||||
| main = print ans | main = print ans | ||||||
| 
 | 
 | ||||||
| ans :: Int | ans :: Integer | ||||||
| ans = fst $ head $ sortOn (Down . snd) $ map collatzChainStartingAt [1..limit] | ans = 42 | ||||||
| 
 | 
 | ||||||
| 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 :: Int -> (Int, Int) | collatzChainStartingAt n = takeWhile (/= 0) $ iterate nextCollatz n | ||||||
| collatzChainStartingAt n = (n, s') |  | ||||||
|   where s' = length $ takeWhile (/= 0) $ iterate nextCollatz n |  | ||||||
| 
 | 
 | ||||||
| limit :: Int | -- solve limit = (n, chain) | ||||||
|  | --   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