Catalan numbers: Difference between revisions

 
(8 intermediate revisions by 5 users not shown)
Line 564:
 
next n</syntaxhighlight>
{{out| Output}}<pre>0 1
0 1
1 1
2 2
Line 579 ⟶ 580:
13 742900
14 2674440
15 9694845</pre>
</pre>
 
==={{header|FreeBASIC}}===
Line 2,308 ⟶ 2,310:
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
procfunc catalan n . ans .
if n = 0
ans = return 1
else .
return call2 * (2 * n - 1) * catalan (n - 1) hdiv (1 + n)
ans = 2 * (2 * n - 1) * h div (1 + n)
.
.
for i = 0 to 14
call print catalan i h
print h
.
</syntaxhighlight>
{{out}}
<pre>
1
1
2
5
14
42
132
429
1430
4862
16796
58786
208012
742900
2674440
</pre>
 
=={{header|EchoLisp}}==
Line 2,973 ⟶ 2,954:
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Catalan_numbers}}
 
'''Solution'''
 
'''Direct definition'''
 
[[File:Fōrmulæ - Catalan numbers 01.png]]
 
[[File:Fōrmulæ - Catalan numbers 02.png]]
 
'''Direct definition (alternative)'''
 
The expression <math>\frac{(2n)!}{(n+1)!\,n!}</math> turns out to be equals to <math>\prod_{k=2}^{n}\frac{n + k}{k}</math>
 
[[File:Fōrmulæ - Catalan numbers 03.png]]
 
(same result)
 
'''No directly defined'''
 
Recursive definitions are easy to write, but extremely inefficient (specially the first one).
 
Because a list is intended to be get, the list of previous values can be used as a form of memoization, avoiding recursion.
 
The next function make use of the "second" form of recursive definition (without recursion):
 
[[File:Fōrmulæ - Catalan numbers 04.png]]
 
[[File:Fōrmulæ - Catalan numbers 05.png]]
 
(same result)
 
=={{header|GAP}}==
Line 3,767 ⟶ 3,778:
=={{header|langur}}==
{{trans|Perl}}
<syntaxhighlight lang="langur">val .factorial = ffn(.x) { if(.x < 2: 1; .x x self(.x - 1)) }
 
val .catalan = ffn(.n) { .factorial(2 x .n) / .factorial(.n+1) / .factorial(.n) }
 
for .i in 0..15 {
Line 6,158 ⟶ 6,170:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./math" for Int
var catalan = Fn.new { |n|
Line 6,221 ⟶ 6,233:
15 9694845
</pre>
 
=={{header|XLISP}}==
<syntaxhighlight lang="lisp">(defun catalan (n)
885

edits