-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCatalanGenerator.hs
More file actions
29 lines (21 loc) · 865 Bytes
/
CatalanGenerator.hs
File metadata and controls
29 lines (21 loc) · 865 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
{- |
Module : CatalanGenerator.hs
Description : Module implements the Catalan 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 CatalanGenerator where
-- NOTE: The following implementation uses the fact that the nth Catalan number
-- is a product of all the numbers of the type (n + k) / k where 1 < k < 2n + 1.
-- | The Catalan sequence generator function
catalanGenerator :: Integer -> [Integer]
catalanGenerator n = take (fromInteger n) [product [i+2..2*i] `div` product [2..i] | i <- [0..]]
main :: IO ()
main = do
putStr "The first 100 elements of the Catalan sequence are "
print (catalanGenerator 100)