forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
catalannumber.go
26 lines (22 loc) · 842 Bytes
/
catalannumber.go
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
// catalannumber.go
// description: Returns the Catalan number
// details:
// In combinatorial mathematics, the Catalan numbers are a sequence of natural numbers that occur in various counting problems, often involving recursively defined objects. - [Catalan number](https://en.wikipedia.org/wiki/Catalan_number)
// The input is the number of the Catalan number n, at the output we get the value of the number
// author(s) [red_byte](https://github.com/i-redbyte)
// see catalannumber_test.go
package catalan
import (
f "github.com/TheAlgorithms/Go/math/factorial"
)
func factorial(n int) int {
result, error := f.Iterative(n)
if error != nil {
panic(error)
}
return result
}
// CatalanNumber This function returns the `nth` Catalan number
func CatalanNumber(n int) int {
return factorial(n*2) / (factorial(n) * factorial(n+1))
}