Advertisement
STANAANDREY

primitive collatz

Feb 9th, 2025
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module CollatzConjecture (collatz) where
  2.  
  3. collatz :: Integer -> Maybe Integer
  4. collatz n
  5.   | n <= 0 = Nothing
  6.   | n == 1 = Just 0
  7.   | even n = addStep $ collatz (n `div` 2)
  8.   | otherwise = addStep $ collatz (3 * n + 1)
  9.   where
  10.     addStep Nothing = Nothing
  11.     addStep (Just x) = Just (x + 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement