Catalan numbers: Difference between revisions

(→‎{{header|Lua}}: Added a Logtalk implementation.)
 
(14 intermediate revisions by 7 users not shown)
Line 563:
print n, " ", c[n]
 
next n</syntaxhighlight>
{{out| Output}}<pre>
 
0 1
end</syntaxhighlight>
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
</pre>
 
==={{header|FreeBASIC}}===
Line 2,294 ⟶ 2,310:
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
func 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,958 ⟶ 2,953:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Catalan_numbers}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
'''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)
 
In '''[https://formulae.org/?example=Catalan_numbers this]''' page you can see the program(s) related to this task and their results.
=={{header|GAP}}==
<syntaxhighlight lang="gap">Catalan1 := n -> Binomial(2*n, n) - Binomial(2*n, n - 1);
Line 3,756 ⟶ 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 3,821 ⟶ 3,844:
 
<syntaxhighlight lang="logtalk">
% libraries
 
:- initialization((
% libraries
logtalk_load(dates(loader)),
logtalk_load(meta(loader)),
logtalk_load(types(loader)),
% application
)).
 
% application
 
:- initialization((
logtalk_load(seqp),
logtalk_load(catalan),
Line 3,850 ⟶ 3,868:
 
:- end_protocol.
end</syntaxhighlight>
 
The implementation of a Catalan sequence generator is in <code>catalan.lgt</code>:
Line 3,856 ⟶ 3,875:
:- object(catalan, implements(seqp)).
 
:- private(catalan/2).
:- dynamic(catalan/2).
 
Line 6,150 ⟶ 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,213 ⟶ 6,233:
15 9694845
</pre>
 
=={{header|XLISP}}==
<syntaxhighlight lang="lisp">(defun catalan (n)
889

edits