Talk:Hailstone sequence: Difference between revisions

Content added Content deleted
m (→‎deleted examples: changed a word in a comment. -- ~~~~)
(A better Haskell version.)
Line 56: Line 56:
::::::* XPL0
::::::* XPL0
<br>No mention was given to explain the reason of the deletion of those two examples. -- [[User:Gerard Schildberger|Gerard Schildberger]] ([[User talk:Gerard Schildberger|talk]]) 20:43, 5 November 2013 (UTC)
<br>No mention was given to explain the reason of the deletion of those two examples. -- [[User:Gerard Schildberger|Gerard Schildberger]] ([[User talk:Gerard Schildberger|talk]]) 20:43, 5 November 2013 (UTC)


==Haskell==

The Haskell version is 18 lines long. This version is 12 lines long and prints all of the chain for n = 27:

import Data.List
import Data.Ord(comparing)
main = do putStrLn $ "Collatz sequence for 27: " ++ ((show.hailstone) 27) ++ "\n"
putStrLn $ "The number " ++ (show longestChain)
++" has the longest hailstone sequence for any number less then 100000."
hailstone n
| n == 1 = [1]
| even n = n:(hailstone (n `div` 2))
| otherwise = n:(hailstone (3*n+1))
longestChain = fst $ maximumBy (comparing snd) $ map ((\x -> (x,(length.hailstone) x))) [1..100000]

Should this be used instead? [[User:Mathlover2|Mathlover2]] ([[User talk:Mathlover2|talk]]) 16:31, 24 February 2015 (UTC)