Catalan numbers/Pascal's triangle: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 14:
[[Pascal's triangle]]
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">V n = 15
V t = [0] * (n + 2)
t[1] = 1
Line 31 ⟶ 30:
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845
</pre>
 
=={{header|360 Assembly}}==
For maximum compatibility, this program uses only the basic instruction set.
<syntaxhighlight lang="360asm">CATALAN CSECT
USING CATALAN,R13,R12
SAVEAREA B STM-SAVEAREA(R15)
Line 141 ⟶ 139:
02674440
09694845</pre>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang=Action"action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Ki
 
DEFINE PTR="CARD"
Line 211 ⟶ 208:
C(15)=9694845
</pre>
 
=={{header|Ada}}==
 
Uses package Pascal from the Pascal triangle solution[[http://rosettacode.org/wiki/Pascal%27s_triangle#Ada]]
 
<syntaxhighlight lang=Ada"ada">with Ada.Text_IO, Pascal;
 
procedure Catalan is
Line 234 ⟶ 230:
 
<pre>1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845</pre>
 
=={{header|ALGOL 68}}==
{{trans|C++}}
<syntaxhighlight lang="algol68">INT n = 15;
[ 0 : n + 1 ]INT t;
t[0] := 0;
Line 251 ⟶ 246:
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845
</pre>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">begin
% print the first 15 Catalan numbers from Pascal's triangle %
integer n;
Line 277 ⟶ 271:
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845
</pre>
 
=={{header|APL}}==
<syntaxhighlight lang="apl">
⍝ Based heavily on the J solution
CATALAN←{¯1↓↑-/1 ¯1↓¨(⊂⎕IO+0 0)⍉¨0 2⌽¨⊂(⎕IO-⍨⍳N){+\⍣⍺⊢⍵}⍤0 1⊢1⍴⍨N←⍵+2}
Line 288 ⟶ 281:
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845
</pre>
 
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}
<syntaxhighlight lang=AutoHotkey"autohotkey">/* Generate Catalan Numbers
//
// smgs: 20th Feb, 2014
Line 325 ⟶ 317:
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845
</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang=AWK"awk">
# syntax: GAWK -f CATALAN_NUMBERS_PASCALS_TRIANGLE.AWK
# converted from C
Line 349 ⟶ 340:
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845
</pre>
 
=={{header|Batch File}}==
<syntaxhighlight lang="dos">@echo off
setlocal ENABLEDELAYEDEXPANSION
set n=15
Line 389 ⟶ 379:
2674440
9694845</pre>
 
=={{header|C}}==
<syntaxhighlight lang="c">
//This code implements the print of 15 first Catalan's Numbers
//Formula used:
Line 443 ⟶ 432:
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845
</pre>
 
=={{header|C sharp|C#}}==
{{trans|C++}}
<syntaxhighlight lang="csharp">
int n = 15;
List<int> t = new List<int>() { 0, 1 };
Line 461 ⟶ 449:
1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">// Generate Catalan Numbers
//
// Nigel Galloway: June 9th., 2012
Line 483 ⟶ 470:
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845
</pre>
 
=={{header|Common Lisp}}==
 
<syntaxhighlight lang=Lisp"lisp">(defun catalan (n)
"Return the n-th Catalan number"
(if (<= n 1) 1
Line 514 ⟶ 500:
2674440
9694845</pre>
 
=={{header|D}}==
{{trans|C++}}
<syntaxhighlight lang="d">void main() {
import std.stdio;
 
Line 538 ⟶ 523:
See [https://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle#Pascal Pascal].
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
(define dim 100)
(define-syntax-rule (Tidx i j) (+ i (* dim j)))
Line 556 ⟶ 541:
</syntaxhighlight>
{{out}}
<syntaxhighlight lang="scheme">
;; take elements on diagonal = Catalan numbers
(for ((i (in-range 0 16))) (write (T (Tidx i i))))
Line 562 ⟶ 547:
→ 1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845
</syntaxhighlight>
 
=={{header|EDSAC order code}}==
Rather than store a triangular array, this solution stores the right-hand half of the current row and updates it in place. It uses the third method in the link, e.g. once we have the half row (70, 56, 28, 8, 1), the Catalan number 42 appears as 70 - 28.
<syntaxhighlight lang="edsac">
[Catalan numbers from Pascal triangle, Rosetta Code website.
EDSAC program, Initial Orders 2]
Line 644 ⟶ 628:
15 9694845
</pre>
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">defmodule Catalan do
def numbers(num) do
{result,_} = Enum.reduce(1..num, {[],{0,1}}, fn i,{list,t0} ->
Line 666 ⟶ 649:
[1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845]
</pre>
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
-module(catalin).
-compile(export_all).
Line 687 ⟶ 669:
Ans=catl(1,2).
</syntaxhighlight>
 
=={{header|ERRE}}==
<syntaxhighlight lang=ERRE"erre">
PROGRAM CATALAN
 
Line 747 ⟶ 728:
15 9694845
</pre>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
let mutable nm=uint64(1)
let mutable dm=uint64(1)
Line 766 ⟶ 746:
printf ", "
</syntaxhighlight>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: arrays grouping io kernel math prettyprint sequences ;
IN: rosetta-code.catalan-pascal
 
Line 786 ⟶ 765:
1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' version 15-09-2015
' compile with: fbc -s console
 
Line 862 ⟶ 840:
14: 2674440
15: 9694845</pre>
 
=={{header|Go}}==
{{trans|C++}}
<syntaxhighlight lang="go">package main
 
import "fmt"
Line 902 ⟶ 879:
15 : 9694845
</pre>
 
=={{header|Groovy}}==
{{trans|C}}
<syntaxhighlight lang=Groovy"groovy">
class Catalan
{
Line 934 ⟶ 910:
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845
</pre>
 
=={{header|Haskell}}==
As required by the task this implementation extracts the Catalan numbers from Pascal's triangle, rather
than calculating them directly. Also, note that it (correctly) produces [1, 1] as the first two numbers.
<syntaxhighlight lang="haskell">import System.Environment (getArgs)
 
-- Pascal's triangle.
Line 963 ⟶ 938:
[1,1,2,5,14,42,132,429,1430,4862,16796,58786,208012,742900,2674440]
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
 
Line 969 ⟶ 943:
that aren't used.
 
<syntaxhighlight lang="unicon">link math
 
procedure main(A)
Line 996 ⟶ 970:
->
</pre>
 
=={{header|J}}==
 
<syntaxhighlight lang="j"> Catalan=. }:@:(}.@:((<0 1)&|:) - }:@:((<0 1)&|:@:(2&|.)))@:(i. +/\@]^:[ #&1)@:(2&+)</syntaxhighlight>
{{out|Example use}}
<syntaxhighlight lang="j"> Catalan 15
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845
</syntaxhighlight>
Line 1,007 ⟶ 980:
A structured derivation of Catalan follows:
 
<syntaxhighlight lang="j"> o=. @: NB. Composition of verbs (functions)
( PascalTriangle=. i. ((+/\@]^:[)) #&1 ) 5
1 1 1 1 1
Line 1,024 ⟶ 997:
Catalan
}:@:(}.@:((<0 1)&|:) - }:@:((<0 1)&|:@:(2&|.)))@:(i. +/\@]^:[ #&1)@:(2&+)</syntaxhighlight>
 
=={{header|Java}}==
{{trans|C++}}
<syntaxhighlight lang="java">public class Test {
public static void main(String[] args) {
int N = 15;
Line 1,049 ⟶ 1,021:
 
<pre>1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845</pre>
 
=={{header|JavaScript}}==
===ES5===
Iteration
{{trans|C++}}
<syntaxhighlight lang="javascript">var n = 15;
for (var t = [0, 1], i = 1; i <= n; i++) {
for (var j = i; j > 1; j--) t[j] += t[j - 1];
Line 1,070 ⟶ 1,041:
{{Trans|Haskell}}
 
<syntaxhighlight lang=JavaScript"javascript">(() => {
'use strict';
 
Line 1,136 ⟶ 1,107:
 
{{Out}}
<syntaxhighlight lang=JavaScript"javascript">[1,2,5,14,42,132,429,1430,4862,16796,58786,208012,742900,2674440,9694845]</syntaxhighlight>
 
=={{header|jq}}==
The first identity (C(2n,n) - C(2n, n-1)) given in the reference is used in accordance with the task description, but it would of course be more efficient to factor out C(2n,n) and use the expression C(2n,n)/(n+1). See also [[Catalan_numbers#jq]] for other alternatives.
Line 1,143 ⟶ 1,113:
''Warning'': jq uses IEEE 754 64-bit arithmetic,
so the algorithm used here for Catalan numbers loses precision for n > 30 and fails completely for n > 510.
<syntaxhighlight lang="jq">def binomial(n; k):
if k > n / 2 then binomial(n; n-k)
else reduce range(1; k+1) as $i (1; . * (n - $i + 1) / $i)
Line 1,154 ⟶ 1,124:
(range(0;16), 30, 31, 510, 511) | [., catalan_by_pascal]
{{Out}}
<syntaxhighlight lang="sh">$ jq -n -c -f Catalan_numbers_Pascal.jq
[0,0]
[1,1]
Line 1,175 ⟶ 1,145:
[510,5.491717746183512e+302]
[511,null]</syntaxhighlight>
 
=={{header|Julia}}==
{{trans|Matlab}}
<syntaxhighlight lang="julia"># v0.6
 
function pascal(n::Int)
Line 1,197 ⟶ 1,166:
{{out}}
<pre>catalan_num(15) = [1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845]</pre>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1.2
 
import java.math.BigInteger
Line 1,243 ⟶ 1,211:
15 : 9694845
</pre>
 
=={{header|Lua}}==
For each line of odd-numbered length from Pascal's triangle, print the middle number minus the one immediately to its right.
This solution is heavily based on the Lua code to generate Pascal's triangle from the page for that task.
<syntaxhighlight lang=Lua"lua">function nextrow (t)
local ret = {}
t[0], t[#t + 1] = 0, 0
Line 1,266 ⟶ 1,233:
{{out}}
<pre>1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440</pre>
 
=={{header|M2000 Interpreter}}==
{{trans|FreeBasic}}
Line 1,277 ⟶ 1,243:
We use & to pass by reference, here anarray, to sub, but because a sub can see anything in module we can change array name inside sub to same as triangle and we can remove arguments (including size).
 
<syntaxhighlight lang=M2000"m2000 Interpreterinterpreter">
Module CatalanNumbers {
Def Integer count, t_row, size=31
Line 1,330 ⟶ 1,296:
15: 9694845
</pre>
 
=={{header|Maple}}==
 
<syntaxhighlight lang="maple">catalan:=proc(n)
local i,a:=[1],C:=[1];
for i to n do
Line 1,345 ⟶ 1,310:
catalan(10);
# [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796]</syntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
This builds the entire Pascal triangle that's needed and holds it in memory. Very inefficienct, but seems to be what is asked in the problem.
<syntaxhighlight lang=Mathematica"mathematica">nextrow[lastrow_] := Module[{output},
output = ConstantArray[1, Length[lastrow] + 1];
Do[
Line 1,366 ⟶ 1,330:
{{out}}
<pre>{1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845}</pre>
 
=={{header|MATLAB}} / {{header|Octave}}==
 
<syntaxhighlight lang=MATLAB"matlab">n = 15;
p = pascal(n + 2);
p(n + 4 : n + 3 : end - 1)' - diag(p, 2)</syntaxhighlight>
Line 1,389 ⟶ 1,352:
2674440
9694845</pre>
 
=={{header|Nim}}==
{{trans|Python}}
<syntaxhighlight lang="nim">const n = 15
var t = newSeq[int](n + 2)
 
Line 1,404 ⟶ 1,366:
{{Out}}
<pre>1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845 </pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">
let catalan : int ref = ref 0 in
Printf.printf "%d ," 1 ;
Line 1,425 ⟶ 1,386:
1 ,2,5,14,42,132,429,1430,4862
</pre>
 
=={{header|Oforth}}==
 
<syntaxhighlight lang=Oforth"oforth">import: mapping
 
: pascal( n -- [] )
Line 1,441 ⟶ 1,401:
[1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845]
</pre>
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">vector(15,n,binomial(2*n,n)-binomial(2*n,n+1))</syntaxhighlight>
{{out}}
<pre>%1 = [1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845]</pre>
 
=={{header|Pascal}}==
<syntaxhighlight lang="pascal">
Program CatalanNumbers
type
Line 1,502 ⟶ 1,460:
 
</pre>
 
=={{header|Perl}}==
{{trans|C++}}
<syntaxhighlight lang=Perl"perl">use constant N => 15;
my @t = (0, 1);
for(my $i = 1; $i <= N; $i++) {
Line 1,518 ⟶ 1,475:
After the 28th Catalan number, this overflows 64-bit integers. We could add <tt>use bigint;</tt> <tt>use Math::GMP ":constant";</tt> to make it work, albeit not at a fast pace. However we can use a module to do it much faster:
{{libheader|ntheory}}
<syntaxhighlight lang=Perl"perl">use ntheory qw/binomial/;
print join(" ", map { binomial( 2*$_, $_) / ($_+1) } 1 .. 1000), "\n";</syntaxhighlight>
 
The <tt>Math::Pari</tt> module also has a binomial, but isn't as fast and overflows its stack after 3400.
 
=={{header|Phix}}==
Calculates the minimum pascal triangle in minimum memory. Inspired by the comments in, but not the code of the FreeBasic example
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">N</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">15</span> <span style="color: #000080;font-style:italic;">-- accurate to 30, nan/inf for anything over 514 (gmp version is below).</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">catalan</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000080;font-style:italic;">-- (&gt;=1 only)</span>
Line 1,546 ⟶ 1,502:
</pre>
Explanatory comments to accompany the above
<syntaxhighlight lang=Phix"phix">-- FreeBASIC said:
--' 1 1 1 1 1 1
--' 1 2 3 4 5 6
Line 1,582 ⟶ 1,538:
=== gmp version ===
{{libheader|Phix/mpfr}}
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,617 ⟶ 1,573:
catalan2m: 0.7s 7.0s 64.9s 644s
</pre>
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">(de bino (N K)
(let f
'((N)
Line 1,633 ⟶ 1,588:
(bino (* 2 N) (inc N)) ) ) )
(bye)</syntaxhighlight>
 
=={{header|PureBasic}}==
{{trans|C}}
<syntaxhighlight lang=PureBasic"purebasic">#MAXNUM = 15
Declare catalan()
 
Line 1,665 ⟶ 1,619:
<pre>
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845</pre>
 
=={{header|Python}}==
===Procedural===
{{trans|C++}}
<syntaxhighlight lang="python">>>> n = 15
>>> t = [0] * (n + 2)
>>> t[1] = 1
Line 1,683 ⟶ 1,636:
 
{{Works with|Python|2.7}}
<syntaxhighlight lang="python">def catalan_number(n):
nm = dm = 1
for k in range(2, n+1):
Line 1,700 ⟶ 1,653:
{{Trans|Haskell}}
{{Works with|Python|3.7}}
<syntaxhighlight lang="python">'''Catalan numbers from Pascal's triangle'''
 
from itertools import (islice)
Line 1,836 ⟶ 1,789:
{{Out}}
<pre>[1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845]</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang=Quackery"quackery"> [ [] 0 rot 0 join
witheach
[ tuck +
Line 1,858 ⟶ 1,810:
 
<pre>1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845</pre>
 
=={{header|Racket}}==
<syntaxhighlight lang=Racket"racket">
#lang racket
 
Line 1,873 ⟶ 1,824:
;; 2674440 9694845)
</syntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2015.12}}
<syntaxhighlight lang="raku" line>constant @pascal = [1], -> @p { [0, |@p Z+ |@p, 0] } ... *;
 
constant @catalan = gather for 2, 4 ... * -> $ix {
Line 1,907 ⟶ 1,857:
1767263190
6564120420</pre>
 
=={{header|REXX}}==
===explicit subscripts===
All of the REXX program examples can handle arbitrary large numbers.
<syntaxhighlight lang="rexx">/*REXX program obtains and displays Catalan numbers from a Pascal's triangle. */
parse arg N . /*Obtain the optional argument from CL.*/
if N=='' | N=="," then N=15 /*Not specified? Then use the default.*/
Line 1,942 ⟶ 1,891:
 
===implicit subscripts===
<syntaxhighlight lang="rexx">/*REXX program obtains and displays Catalan numbers from a Pascal's triangle. */
parse arg N . /*Obtain the optional argument from CL.*/
if N=='' | N=="," then N=15 /*Not specified? Then use the default.*/
Line 1,958 ⟶ 1,907:
 
===using binomial coefficients===
<syntaxhighlight lang="rexx">/*REXX program obtains and displays Catalan numbers from a Pascal's triangle. */
parse arg N . /*Obtain the optional argument from CL.*/
if N=='' | N=="," then N=15 /*Not specified? Then use the default.*/
Line 1,975 ⟶ 1,924:
===binomial coefficients, memoized===
This REXX version uses memoization for the calculation of factorials.
<syntaxhighlight lang="rexx">/*REXX program obtains and displays Catalan numbers from a Pascal's triangle. */
parse arg N . /*Obtain the optional argument from CL.*/
if N=='' | N=="," then N=15 /*Not specified? Then use the default.*/
Line 1,991 ⟶ 1,940:
if x-y<y then y=x-y; _=1; do j=x-y+1 to x; _=_*j; end; return _/!(y)</syntaxhighlight>
'''output''' &nbsp; is the same as the 1<sup>st</sup> version. <br><br>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
n=15
cat = list(n+2)
Line 2,012 ⟶ 1,960:
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="tcl">def catalan(num)
t = [0, 1] #grows as needed
(1..num).map do |i|
Line 2,029 ⟶ 1,976:
[1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845]
</pre>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">n = 15
dim t(n+2)
t(1) = 1
Line 2,042 ⟶ 1,988:
{{out}}
<pre>1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845 </pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
 
fn main()
Line 2,076 ⟶ 2,021:
{{out}}
<pre>1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845 </pre>
 
=={{header|Scala}}==
<syntaxhighlight lang=Scala"scala">def catalan(n: Int): Int =
if (n <= 1) 1
else (0 until n).map(i => catalan(i) * catalan(n - i - 1)).sum
Line 2,084 ⟶ 2,028:
(1 to 15).map(catalan(_))</syntaxhighlight>
{{Out}}See it in running in your browser by [https://scastie.scala-lang.org/2ybpRZxCTOyrx3mIy8yIDw Scastie (JVM)].
 
=={{header|Scilab}}==
<syntaxhighlight lang="text">n=15
t=zeros(1,n+2)
t(1)=1
Line 2,115 ⟶ 2,058:
2674440.
9694845. </pre>
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
Line 2,143 ⟶ 2,085:
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845
</pre>
 
=={{header|Sidef}}==
{{trans|Ruby}}
<syntaxhighlight lang="ruby">func catalan(num) {
var t = [0, 1]
(1..num).map { |i|
Line 2,159 ⟶ 2,100:
{{out}}
<pre>1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845</pre>
 
=={{header|smart BASIC}}==
<syntaxhighlight lang="qbasic">PRINT "Catalan Numbers from Pascal's Triangle"!PRINT
x = 15
DIM t(x+2)
Line 2,175 ⟶ 2,115:
PRINT n,"#######":t(n+1) - t(n)
NEXT n</syntaxhighlight>
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">proc catalan n {
set result {}
array set t {0 0 1 1}
Line 2,192 ⟶ 2,131:
{{out}}
<pre>1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845</pre>
 
=={{header|TI-83 BASIC}}==
<syntaxhighlight lang="ti83b">"CATALAN
15→N
seq(0,I,1,N+2)→L1
Line 2,225 ⟶ 2,163:
9694845
Done </pre>
 
=={{header|Vala}}==
{{trans|C++}}
<syntaxhighlight lang="vala">void main() {
const int N = 15;
uint64[] t = {0, 1};
Line 2,244 ⟶ 2,181:
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845
</pre>
 
=={{header|VBScript}}==
To run in console mode with cscript.
<syntaxhighlight lang="vbscript">dim t()
if Wscript.arguments.count=1 then
n=Wscript.arguments.item(0)
Line 2,267 ⟶ 2,203:
Wscript.echo t(i+1)-t(i)
next 'i</syntaxhighlight>
 
=={{header|Visual Basic}}==
{{trans|Rexx}}
{{works with|Visual Basic|VB6 Standard}}
<syntaxhighlight lang="vb">
Sub catalan()
Const n = 15
Line 2,307 ⟶ 2,242:
9694845
</pre>
 
=={{header|Wren}}==
{{trans|C++}}
<syntaxhighlight lang="ecmascript">var n = 15
var t = List.filled(n+2, 0)
t[1] = 1
Line 2,325 ⟶ 2,259:
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845
</pre>
 
=={{header|Zig}}==
Uses comptime functionality to compile Pascal's triangle into the object code, then at runtime, it's simply a table lookup. (uses code from AKS primality task.)
<syntaxhighlight lang=Zig"zig">
const std = @import("std");
const stdout = std.io.getStdOut().outStream();
Line 2,390 ⟶ 2,323:
15 9694845
</pre>
 
=={{header|zkl}}==
{{trans|PARI/GP}} using binomial coefficients.
<syntaxhighlight lang="zkl">fcn binomial(n,k){ (1).reduce(k,fcn(p,i,n){ p*(n-i+1)/i },1,n) }
(1).pump(15,List,fcn(n){ binomial(2*n,n)-binomial(2*n,n+1) })</syntaxhighlight>
{{out}}
Line 2,399 ⟶ 2,331:
L(1,2,5,14,42,132,429,1430,4862,16796,58786,208012,742900,2674440,9694845)
</pre>
 
=={{header|ZX Spectrum Basic}}==
{{trans|C++}}
<syntaxhighlight lang="zxbasic">10 LET N=15
20 DIM t(N+2)
30 LET t(2)=1
10,327

edits