Catalan numbers: Difference between revisions
→{{header|langur}}
Ttmrichter (talk | contribs) (→{{header|Lua}}: Added a Logtalk implementation.) |
Langurmonkey (talk | contribs) |
||
(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
if n = 0
return
.
for i = 0 to 14
.
</syntaxhighlight>
▲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}}
'''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}}==
<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 =
val .catalan =
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
logtalk_load(seqp),
logtalk_load(catalan),
Line 3,850 ⟶ 3,868:
:- end_protocol.
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="
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)
|