Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
32
Task/Hailstone-sequence/Haskell/hailstone-sequence-1.hs
Normal file
32
Task/Hailstone-sequence/Haskell/hailstone-sequence-1.hs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import Data.List (maximumBy)
|
||||
import Data.Ord (comparing)
|
||||
|
||||
-------------------- HAILSTONE SEQUENCE ------------------
|
||||
|
||||
collatz :: Int -> Int
|
||||
collatz n
|
||||
| even n = n `div` 2
|
||||
| otherwise = 1 + 3 * n
|
||||
|
||||
hailstone :: Int -> [Int]
|
||||
hailstone = takeWhile (1 /=) . iterate collatz
|
||||
|
||||
longestChain :: Int
|
||||
longestChain =
|
||||
fst $
|
||||
maximumBy (comparing snd) $
|
||||
(,) <*> (length . hailstone) <$> [1 .. 100000]
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
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)
|
||||
]
|
||||
26
Task/Hailstone-sequence/Haskell/hailstone-sequence-2.hs
Normal file
26
Task/Hailstone-sequence/Haskell/hailstone-sequence-2.hs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
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)
|
||||
|
||||
withResult :: (Int -> Int) -> Int -> (Int, Int)
|
||||
withResult f x = (f x, x)
|
||||
|
||||
h27 :: [Int]
|
||||
h27 = hailstone 27
|
||||
|
||||
main :: IO ()
|
||||
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]
|
||||
]
|
||||
51
Task/Hailstone-sequence/Haskell/hailstone-sequence-3.hs
Normal file
51
Task/Hailstone-sequence/Haskell/hailstone-sequence-3.hs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import Data.List (unfoldr)
|
||||
|
||||
|
||||
-------------------- HAILSTONE SEQUENCE ------------------
|
||||
|
||||
hailStones :: Int -> [Int]
|
||||
hailStones = (<> [1]) . unfoldr go
|
||||
where
|
||||
f x
|
||||
| even x = div x 2
|
||||
| otherwise = 1 + 3 * x
|
||||
go x
|
||||
| 2 > x = Nothing
|
||||
| otherwise = Just (x, f x)
|
||||
|
||||
mostStones :: Int -> (Int, Int)
|
||||
mostStones = foldr go (0, 0) . enumFromTo 1
|
||||
where
|
||||
go x (m, ml)
|
||||
| l > ml = (x, l)
|
||||
| otherwise = (m, ml)
|
||||
where
|
||||
l = length (hailStones x)
|
||||
|
||||
------------------------- 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
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue