nth_catalan

Ali Hamdi Ali Fadel · @AliOsm · almost 3 years

Calculates the n-th Catalan number, (2N)! / (N! * (N + 1)!), in a mathematical operations efficient way.

def nth_catalan(n)
result = 1
m = n + 2
while m <= 2 * n do
result = result * m / (m - n - 1)
m += 1
end
result /= (m - n - 1)
end
0 · 0 · 2