-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollatzGenerator.hs
More file actions
32 lines (25 loc) · 900 Bytes
/
CollatzGenerator.hs
File metadata and controls
32 lines (25 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{- |
Module : ColatzGenerator.hs
Description : Module implements the Collatz number series generation algorithm
Copyright : (c) David Oniani
License : MIT License
Maintainer : onianidavid@gmail.com
Stability : provisional
Portability : portable
For more information, follow the link below.
https://en.wikipedia.org/wiki/Catalan_number
-}
module CollatzGenerator where
-- | The Collatz sequence generator function
collatzGenerator :: Integer -> [Integer]
collatzGenerator n = collatzGenerator' n []
where
collatzGenerator' :: Integer -> [Integer] -> [Integer]
collatzGenerator' n x
| n == 1 = x ++ [n]
| even n = collatzGenerator' (div n 2) (x ++ [n])
| odd n = collatzGenerator' (3 * n + 1) (x ++ [n])
main :: IO ()
main = do
putStr "The Collatz sequence for the number 100 is "
print (collatzGenerator 100)