Jump to content

Catalan numbers: Difference between revisions

(Updated D entry)
Line 1,467:
 
=={{header|Julia}}==
<lang julia>catalanCatalan(n::Integer) = intdiv(binomial(2n, n)/(, n+1))</lang>
From the Catalan package, returns the n-th Catalan number
<lang julia>function catalan(bn::Integer)
if bn < 0
throw(DomainError())
else
n = BigInt(bn)
end
return binomial(2n, n)/(n + 1)
end</lang>
{{out}}
<pre>julia> [Catalan(n) for n = 1:15 println(catalan(n)) end]
15-element Array{Int64,1}:
1
if bn < 01
2
else 2
5
end 5
14
14
42
42
132
132
429
429
1430
4862
16796
58786
208012
742900
2674440
9694845</pre>
We can also use the binomial function. Beware, by default, integers are int64 integers with limited precision. One should pass a BigInt integer as an argument to use arbitrary precision integers in the computation, and avoid overflow for big values of n.
 
<lang julia>catalan(n) = int(binomial(2n,n)/(n+1))</lang>
julia> Catalan(big(100))
{{Out}}
896519947090131496687170070074100632420837521538745909320
<pre>julia> for i=1:15 println(catalan(big(i))) end
...</pre>
1
(In the second example, we have used arbitrary-precision integers to avoid overflow for large Catalan numbers.)
2
...</pre>
 
=={{header|K}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.