September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,18 +1,26 @@
import Data.List (maximumBy)
import Data.Ord (comparing)
main = do putStrLn $ "Collatz sequence for 27: "
++ ((show.hailstone) 27)
++ "\n"
++ "The number "
++ (show longestChain)
++" has the longest hailstone sequence"
++" for any number less then 100000. "
++"The sequence has length "
++ (show.length.hailstone $ longestChain)
hailstone :: Int -> [Int]
hailstone = takeWhile (/= 1) . iterate collatz
where
collatz n =
if even n
then n `div` 2
else 3 * n + 1
hailstone = takeWhile (/=1) . (iterate collatz)
where collatz n = if even n then n `div` 2 else 3*n+1
longestChain :: Int
longestChain =
fst
(maximumBy (comparing snd) (((,) <*> length . hailstone) <$> [1 .. 100000]))
longestChain = fst $ maximumBy (comparing snd) $
map ((\x -> (x,(length.hailstone) x))) [1..100000]
--TEST -------------------------------------------------------------------------
main =
mapM_
putStrLn
[ "Collatz sequence for 27: "
, (show . hailstone) 27
, "The number " ++ show longestChain
, "has the longest hailstone sequence for any number less then 100000. "
, "The sequence has length: " ++ (show . length . hailstone $ longestChain)
]

View file

@ -1,19 +1,26 @@
import Data.List (maximumBy)
import Data.Ord (comparing)
import Data.List (maximumBy, intercalate)
hailstone :: Int -> [Int]
hailstone 1 = [1]
hailstone n | even n = n : hailstone (n `div` 2)
| otherwise = n : hailstone (n * 3 + 1)
hailstone 1 = [1]
hailstone n
| even n = n : hailstone (n `div` 2)
| otherwise = n : hailstone (n * 3 + 1)
withResult :: (t -> t1) -> t -> (t1, t)
withResult :: (Int -> Int) -> Int -> (Int, Int)
withResult f x = (f x, x)
h27 :: [Int]
h27 = hailstone 27
main :: IO ()
main = do
let h27 = hailstone 27
print $ length h27
let h4 = show $ take 4 h27
let t4 = show $ drop (length h27 - 4) h27
putStrLn ("hailstone 27: " ++ h4 ++ " ... " ++ t4)
print $ maximumBy (comparing fst) $ map (withResult (length . hailstone)) [1..100000]
main =
mapM_
putStrLn
[ (show . length) h27
, "hailstone 27: " ++
intercalate " ... " (show <$> [take 4 h27, drop (length h27 - 4) h27])
, show $
maximumBy (comparing fst) $
withResult (length . hailstone) <$> [1 .. 100000]
]

View file

@ -0,0 +1,53 @@
import Data.List (unfoldr)
hailStones :: Int -> [Int]
hailStones =
(++ [1]) .
unfoldr
(\x ->
if x < 2
then Nothing
else Just
( x
, if even x
then div x 2
else (3 * x) + 1))
mostStones :: Int -> (Int, Int)
mostStones n =
foldr
(\x (m, ml) ->
let l = length (hailStones x)
in if l > ml
then (x, l)
else (m, ml))
(0, 0)
[1 .. n]
-- GENERIC -------------------------------------------------------------------
lastN_ :: Int -> [Int] -> [Int]
lastN_ = (foldr (const (drop 1)) <*>) . drop
-- TEST -----------------------------------------------------------------------
h27, start27, end27 :: [Int]
[h27, start27, end27] = [id, take 4, lastN_ 4] <*> [hailStones 27]
maxNum, maxLen :: Int
(maxNum, maxLen) = mostStones 100000
main :: IO ()
main =
mapM_
putStrLn
[ "Sequence 27 length:"
, show $ length h27
, "Sequence 27 start:"
, show start27
, "Sequence 27 end:"
, show end27
, ""
, "N with longest sequence where N <= 100000"
, show maxNum
, "length of this sequence:"
, show maxLen
]