Jump to content

Catalan numbers/Pascal's triangle: Difference between revisions

jq
(→‎{{header|Perl}}: Add notes about overflow, and use a module)
(jq)
Line 165:
Catalan
}:@:(}.@:((<0 1)&|:) - }:@:((<0 1)&|:@:(2&|.)))@:(i. +/\@]^:[ #&1)@:(2&+)</lang>
=={{header|jq}}==
The first identity (C(2n,n) - C(2n, n-1)) given in the reference is used, as required
by the task description, but see [[Catalan_numbers#jq]] for better alternatives.
 
<lang jq># Warning: jq uses IEEE 754 64-bit arithmetic,
# so the algorithm loses precision for n > 30 and fails completely for n > 510.
 
def catalan_by_pascal: . as $n | binomial(2*$n; $n) - binomial(2*$n; $n-1);</lang>
 
'''Example''':
(range(0;16), 30, 31, 510, 511) | [., catalan_by_pascal]
{{Out}}
<lang sh>$ jq -n -c -f Catalan_numbers_Pascal.jq.bak
[0,0]
[1,1]
[2,2]
[3,5]
[4,14]
[5,42]
[6,132]
[7,429]
[8,1430]
[9,4862]
[10,16796]
[11,58786]
[12,208012]
[13,742900]
[14,2674440]
[15,9694845]
[30,3814986502092304]
[31,14544636039226880]
[510,5.491717746183512e+302]
[511,null]</lang>
 
=={{header|Mathematica}}==
2,489

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.