-
Notifications
You must be signed in to change notification settings - Fork 11
/
catalan.go
31 lines (23 loc) · 818 Bytes
/
catalan.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
27
28
29
30
31
//The Catalan numbers are a sequence of positive integers that appear in many counting
//problems in combinatorics.
//reference: https://brilliant.org/wiki/catalan-numbers/
package dynamic
import "fmt"
var errCatalan = fmt.Errorf("can't have a negative n-th catalan number")
// NthCatalan returns the n-th Catalan Number
// Complexity: O(n²)
func NthCatalanNumber(n int) (int64, error) {
if n < 0 {
//doesn't accept negative number
return 0, errCatalan
}
var catalanNumberList []int64
catalanNumberList = append(catalanNumberList, 1) //first value is 1
for i := 1; i <= n; i++ {
catalanNumberList = append(catalanNumberList, 0) //append 0 and calculate
for j := 0; j < i; j++ {
catalanNumberList[i] += catalanNumberList[j] * catalanNumberList[i-j-1]
}
}
return catalanNumberList[n], nil
}