| Bytes | Lang | Time | Link |
|---|---|---|---|
| 008 | 05AB1E | 250514T021006Z | Lucenapo |
| 063 | Raku Perl 6 rakudo | 241212T170232Z | xrs |
| 009 | Brachylog | 230222T064549Z | Unrelate |
| 006 | Thunno 2 | 230623T165748Z | The Thon |
| 006 | Nekomata | 230416T025406Z | alephalp |
| 033 | C | 151209T213730Z | Stefano |
| 063 | tinylisp | 220419T180858Z | DLosc |
| 064 | tinylisp | 220218T021113Z | Axuary |
| 028 | GAP | 220218T014327Z | David Sc |
| 028 | Factor + math.combinatorics | 220217T160936Z | chunes |
| 005 | Vyxal r | 220205T113514Z | math sca |
| 010 | Japt | 201005T091922Z | Shaggy |
| 009 | Husk | 201005T082306Z | Razetime |
| 017 | Pari/GP | 151210T050920Z | alephalp |
| 001 | Pyt | 180108T151118Z | mudkip20 |
| 078 | BrainFlak | 180111T003506Z | Nitrodon |
| 086 | Lua | 171110T135523Z | MCAdvent |
| 231 | TeX | 170820T170902Z | Leaky Nu |
| 023 | Excel | 170804T102619Z | Wernisch |
| 013 | cQuents | 170626T190500Z | Stephen |
| 016 | Alice | 170412T124536Z | Martin E |
| 045 | Clojure | 161228T001056Z | NikoNyrh |
| 009 | Oasis | 160831T121839Z | Leaky Nu |
| 092 | Python 2 | 160808T111601Z | Leaky Nu |
| nan | ><> | 160805T092122Z | Sok |
| 028 | R | 160805T090658Z | Rudier |
| 012 | Jellyfish | 160805T025616Z | Leaky Nu |
| 006 | 05AB1E | 151223T021308Z | Adnan |
| 018 | Maple | 160729T164958Z | DSkoog |
| 068 | Sesos | 160723T185533Z | Leaky Nu |
| 008 | MATL | 160224T115420Z | Luis Men |
| 016 | R | 151211T101935Z | mnel |
| 036 | 𝔼𝕊𝕄𝕚𝕟 | 151224T022405Z | Mama Fun |
| 223 | Javagony | 151228T143426Z | flawr |
| 004 | pl | 151221T154958Z | a spaghe |
| 060 | Ceylon | 151212T175515Z | Paŭlo Eb |
| 022 | Octave | 151210T051906Z | alephalp |
| 042 | Prolog | 151211T115707Z | Emigna |
| 030 | Ruby | 151211T092428Z | Vasu Ada |
| 108 | C | 151209T192115Z | cleblanc |
| 053 | Clojure/ClojureScript | 151210T165700Z | MattPutn |
| 013 | Jolf | 151209T193828Z | Conor O& |
| 004 | Jelly | 151209T173959Z | Dennis |
| 025 | Matlab | 151209T174410Z | costrom |
| 014 | Milky Way 1.5.14 | 151209T212857Z | Zach Gat |
| 013 | Vitsy | 151209T194938Z | Addison |
| 009 | Seriously | 151209T185752Z | quintopi |
| 055 | Python 3 | 151209T174408Z | Sherlock |
| 013 | Mathematica | 151209T174124Z | a spaghe |
| 023 | Julia | 151209T174951Z | Alex A. |
| 008 | Pyth | 151209T174458Z | FryAmThe |
| 016 | Japt | 151209T175224Z | ETHprodu |
| 024 | JavaScript ES6 | 151209T181227Z | ETHprodu |
| 009 | Dyalog APL | 151209T175020Z | lirtosia |
| 033 | Python 3 | 151209T175846Z | xnor |
| 008 | J | 151209T175452Z | lirtosia |
| 027 | Haskell | 151209T175228Z | xnor |
| 012 | CJam | 151209T174130Z | Martin E |
| 011 | TIBASIC | 151209T174348Z | lirtosia |
05AB1E, 8 bytes
xD;>rcr/
Explanation:
Code: Stack: Explanation:
xD;>rcr/
x [n, 2n] # Pops a, push a, a * 2
D [n, 2n, 2n] # Pops a, pushes a, a
; [n, 2n, n] # Pops a, pushes a / 2
> [n, 2n, n+1] # Pops a, pushes a + 1
r [n+1, 2n, n] # Reverse stack
c [n+1, 2n nCr n] # Pops a, b, pushes a nCr b
r [2n nCr n, n+1] # Reverse stack
/ [(2n nCr n)/(n+1)] # Pops a, b, pushes a/b
# Implicit, nothing has printed, so we print the last item
Raku (Perl 6) (rakudo), 63 bytes
my $n=@*ARGS[0];say ([*] 1..(2*$n))/([*] 1..($n+1))/([*] 1..$n)
AWK, 67 bytes
func f(n){for(r=i=1;i++<n;)r*=i;return r}1,$0=f(2*$1)/f($1+1)/f($1)
Brachylog, 9 bytes
⟦k⟦ᵐẋ≤₁ˢl
Probably does beat any direct adaptation of a formula... both in length, and in memory usage, since it stores \$n!\$ lattice paths simultaneously before filtering them to monotonically non-decreasing. ⟦{{⟦₃∋}ˡ}ᶜ fares much better for an extra byte.
ẋ Take the Cartesian product of
⟦ [0 .. m]
⟦k ᵐ for every m in [0 .. n).
≤₁ˢ Filter to nondecreasing.
l How many are left?
Brachylog, 15 bytes
×₂{g{b|~b}ⁱ⁾Ė}ᶜ
Effectively brute forces all Dyck words of length \$2n\$. I don't know that an adaptation of one of the formulas can't be shorter, but my handful of (failed) attempts have all been roughly this length.
{ }ᶜ Count the number of possible ways to:
ⁱ iterate
g ⁾ starting with the empty list
×₂ ⁾ 2n times,
b removing the first element
{ |~b} or adding an element
Ė such that the end result is the empty list.
Thunno 2, 6 bytes
Ḍscs⁺÷
Explanation
Ḍscs⁺÷ # Implicit input -> n
Ḍ # Double -> 2n
sc # nCr with input -> (2n)C(n)
s⁺ # Increment input -> (2n)C(n), n+1
÷ # Divide -> ((2n)C(n)) / (n+1)
# Implicit output
Nekomata, 6 bytes
+$Ç$→/
+$Ç$→/
+ Add the input with itself
$ Swap
Ç Binomial coefficient
$ Swap
→ Increment
/ Divide
Nekomata + -n, 12 bytes
+Ø$ᵑ{ᵉçt?}Ø=
A port of @Unrelated String's Brachylog answer.
+Ø$ᵑ{ᵉçt?}Ø=
Ø$ Starting from the empty list
+ ᵑ{ } Apply the following function 2*input times
ᵉç Prepend zero
t? Or drop the first element
Ø= Check if the result is empty
The -n flag counts the number of possible results.
C, 78 52 39 34 33 bytes
Even more C magic (thanks xsot):
c(n){return!n?:(4+6./~n)*c(n-1);}
This time by expanding the recurrence below (thanks xnor and Thomas Kwa):
$$ \begin{equation} \begin{split} C_0 & = 1 \\ C_n & = \frac{2 (2n - 1)}{n + 1} C_{n - 1} \\ & = \frac{2 (2n+2-3)}{n+1} C_{n - 1} \\ & = 2 \left(2\frac{n+1}{n+1} - \frac{3}{n+1}\right) C_{n - 1} \\ & = \left(4 - \frac{6}{n+1}\right) C_{n - 1} \end{split} \end{equation} $$
c(n){return n?(4+6./~n)*c(n-1):1;}
-(n+1) is replaced by ~n, which is equivalent in two's complement and saves 4 bytes.
Again as a function, but this time exploiting the following recurrence:
$$ \begin{equation} \begin{split} C_0 & = 1 \\ C_n & = \frac{2 (2n - 1)}{n + 1} \cdot C_{n - 1} \end{split} \end{equation} $$
c(n){return n?2.*(2*n++-1)/n*c(n-2):1;}
c(n) enters an infinite recursion for negative n, although it's not relevant for this challenge.
Since calling a function seems an acceptable alternative to console I/O:
c(n){double c=1,k=2;while(k<=n)c*=1+n/k++;return c;}
c(n) takes an int and returns an int.
Original entry:
main(n){scanf("%d",&n);double c=1,k=2;while(k<=n)c*=1+n/k++;printf("%.0f",c);}
Instead of directly calculating the definition, the formula is rewritten as:
$$ \begin{equation} \begin{split} \frac{1}{n + 1} {2n \choose n} &= \frac{(2n)!}{(n!)^2 \cdot (n + 1)} \\ & = \frac{2n \cdot \ldots \cdot (n + 1)}{n! \cdot (n + 1)} \\ & = \frac{1}{n + 1} \cdot \frac{\prod_{k = 1}^n (n + k)}{\prod_{k = 1}^n k} \\ & = \frac{1}{n + 1} \cdot \prod_{k = 1}^n \frac{n + k}{k} \\ & = \frac{1}{n + 1} \cdot \prod_{k = 1}^n \left(1 + \frac{n}{k}\right) \\ & = \frac{1}{n + 1} (n + 1) \prod_{k = 2}^n \left(1 + \frac{n}{k}\right) \\ & = \prod_{k = 2}^n \left(1 + \frac{n}{k}\right) \end{split} \end{equation} $$
The formula assumes n >= 2, but the code accounts for n = 0 and n = 1 too.
In the C mess above, n and k have the same role as in the formula, while c accumulates the product. All calculations are performed in floating point using double, which is almost always a bad idea, but in this case the results are correct up to n = 19 at least, so it's ok.
float would have saved 1 byte, unfortunately it's not precise enough.
tinylisp, 63 bytes
(load library
(d C(q((N)(i N(/(*(C(dec N))(s(* 4 N)2))(inc N))1
Explanation
Uses one of the recurrence relations from Stefano Sanfilippo's C answer:
$$ \begin{align} C_0 & = 1 \\ C_n & = C_{n-1} \cdot \frac{4n-2}{n+1} \end{align} $$
(d C(q((N)(i N(/(*(C(dec N))(s(* 4 N)2))(inc N))1
(d C Define C
(q((N) as a function of N:
(i N If N is nonzero:
(/ ) Divide
(* ) the product of
(C(dec N)) a recursive call with N-1
(s(* 4 N)2) and (4*N)-2
(inc N) by N-1
1 Else, return 1
tinylisp, 72 67 64 bytes
-5 bytes thanks to @Giuseppe -3 bytes thanks to @DLosc
(load library
(d F factorial
(d g(q((n)(/(F(+ n n))(F n)(F(+ n 1
Vyxal r, 6 5 bytes
I had to use the r flag. It's only useful once in a blue moon.
‹?dƈ/
Formula: \$\huge{\frac{{2n \choose n-1}}{n}}\$
Explanation:
‹ decrement n
? push n again
d double n
ƈ push 2n choose n-1 (because reversed)
/ divide by n
Brain-Flak, 78 bytes
(([{}])<(({}){}){({}()<(())<>{({}({})<>)<>}>)}><>){({}()<{}>)}{}({}[{}]<>{}{})
Computes the first 2n rows of Pascal's triangle, then returns ((2n-1) C n) - ((2n-1) C (n+1)).
Lua, 86 bytes
c=setmetatable({[0]=1},{__index=function(c,n)c[n]=c[n-1]*(4-(6/(n+1)))print(c[n])end})
TeX, 231 bytes
\newcommand{\f}[1]{\begingroup\count0=1\count1=2\count2=2\count3=0\loop\multiply\count0 by\the\count1\divide\count0 by\the\count2\advance\count1 by4\advance\count2 by1\advance\count3 by1
\ifnum\count3<#1\repeat\the\count0\endgroup}
Usage
\documentclass[12pt,a4paper]{article}
\begin{document}
\newcommand{\f}[1]{\begingroup\count0=1\count1=2\count2=2\count3=0\loop\multiply\count0 by\the\count1\divide\count0 by\the\count2\advance\count1 by4\advance\count2 by1\advance\count3 by1
\ifnum\count3<#1\repeat\the\count0\endgroup}
\newcount \i
\i = 0
\loop
\f{\the\i}
\advance \i by 1
\ifnum \i < 15 \repeat
\end{document}
Excel, 23 bytes
Unimaginative answer:
=COMBIN(2*A1,A1)/(A1+1)
cQuents, 13 bytes
f2$)/(f$+1)f$
1-indexed. Try it online!
Explanation
: Implicit; sets mode to sequence
Given input n, output nth term in sequence; otherwise, output sequence
Each term in the sequence is equal to:
f2$) Factorial (2 * current)
/ Divided by
( ) Group (implicit )
f$+1) Factorial (current + 1)
Implicit multiplication
f$) Factorial (current, implicit )
Alice, 16 bytes
/o
\i@/..2*~C~h:
Explanation
This is a basic framework for arithmetic programs to read and write integer I/O and process them in Cardinal mode:
/o
\i@/...
As for the actual computation, I'm using the usual formula given in the challenge:
.. Make two copies of the input.
2* Double one copy.
~ Swap it with another copy.
C Compute the binomial coefficient (2n,n).
~ Swap with the third copy.
h Increment.
: Divide to compute C_n = (2n,n)/(n+1)
Clojure, 45 bytes
#(apply *(for[k(range 2(inc %))](/(+ % k)k)))
Uses the direct formula instead of recursion. Luckily * with zero arguments returns one :)
Oasis, 9 bytes
nxx«*n>÷1
Oasis is a language designed by Adnan which is specialized in sequences.
Here, we shall use the following relationship kindly provided by Stefano Sanfilippo:

Currently, this language can do recursion and closed form.
To specify that a(0)=1 is simple: just add the 1 at the end.
For example, if a sequence begins with a(0)=0 and a(1)=1, just put 10 at the end.
Unfortunately, all sequences must be 0-indexed.
nxx«*n>÷1 stack
1 a(0)=1
n push n (input) n
x double 2n
x double 4n
« minus 2 4n-2
* multiply: second (4n-2)*a(n-1)
argument is missing,
so a(n-1) is used.
n push n (input) (4n-2)*a(n-1) n
> add 1 (4n-2)*a(n-1) n+1
÷ integer division (4n-2)*a(n-1)/(n+1)
= ((4n-2)/(n+1))*a(n-1)
= ((4n+4-6)/(n+1))*a(n-1)
= ((4n+4)/(n+1) - 6/(n+1))*a(n-1)
= (4-6/(n+1))*a(n-1)
Closed-form:
10 bytes
nx!n!n>!*÷
nx!n!n>!*÷
n push n (input)
x double
! factorial: stack is now [(2n)!]
n push n (input)
! factorial: stack is now [(2n)! n!]
n push n (input)
> add 1
! factorial: stack is now [(2n)! n! (n+1)!]
* multiply: stack is now [(2n)! (n!(n+1)!)]
÷ divide: stack is now [(2n)!/(n!(n+1)!)]
Python 2, 92 bytes
(lambda f:lambda n:f(2*n)/f(n)/f(n+1))((lambda r:r(r))(lambda r:lambda x:x<1or x*r(r)(x-1)))
This answer purely serves to demonstrate the power of lambdas.
><>, 29+2 = 31 bytes
:1+$?!\:2-
$/?=1l<*-$4,$6
;\n
Requires the input to be present on the stack at program start, so +2 bytes for the -v flag. Try it online!
Uses the algorithm presented in Stefano Sanfilippo's answer (linky).
The first line compiles n+1, n, n-1, ... , 2, 1 on the stack. The second line, running backwards, computes C(n) = (4 - (6 / (n+1)) * C(n-1). Each iteration reduces the size of the stack by 1, so the remaining number is output when the length of the stack is 1.
R, 28 bytes
Not using a package, so slightly longer than a previous answer
choose(2*(n=scan()),n)/(n+1)
05AB1E, 6 bytes
Dxcr>/
Explanation:
Code: Stack: Explanation:
Dxcr>/
D [n, n] # Duplicate of the stack. Since it's empty, input is used.
x [n, n, 2n] # Pops a, pushes a, a * 2
c [n, n nCr 2n] # Pops a,b pushes a nCr b
r [n nCr 2n, n] # Reverses the stack
> [n nCr 2n, n + 1] # Increment on the last item
/ [(n nCr 2n)/(n + 1)] # Divides the last two items
# Implicit, nothing has printed, so we print the last item
Maple, 18 bytes
(2*n)!/((n+1)!*n!)
Usage:
> f := n->(2*n)!/((n+1)!*n!);
> f(19);
1767263190
Sesos, 94 86 68 bytes
8 bytes by changing the factorial-er from version 1 to version 2.
18 bytes by computing n!(n+1)! in one step. Largely inspired by Dennis' primality test algorithm.
Hexdump:
0000000: 16f8de a59f17 a0ebba 7f4cd3 e05f3f cf0fd0 a0ebde ..........L.._?......
0000015: b1c1bb 76fe18 8cc1bb 76fe1c e0fbda 390fda bde3d8 ...v.....v.....9.....
000002a: 000fbe af9d1b b47bc7 cfc11c b47bc7 cff1fa e07bda .......{.....{.....{.
000003f: 39e83e cf07 9.>..
Uses the formula a(n) = (2n)! / (n!(n+1)!).
- The factorial-er: version 1 (in-place, constant memory), version 2 (in-place, linear memory)
- The multiplier: here (in place, constant memory)
- The divider: here (does not halt if not divisible)
Assembler
set numin
set numout
get
jmp,sub 1,fwd 1,add 1,fwd 2,add 2,rwd 3,jnz
fwd 1,add 1
jmp
jmp,sub 1,rwd 1,add 1,rwd 1,add 1,rwd 1,add 1,fwd 3,jnz
rwd 1,sub 1,rwd 1,sub 1,rwd 1
jmp,sub 1,fwd 3,add 1,rwd 3,jnz
fwd 1
jnz
fwd 3
jmp
jmp
sub 1,rwd 1
jmp,sub 1,rwd 1,add 1,rwd 1,add 1,fwd 2,jnz
rwd 2
jmp,sub 1,fwd 2,add 1,rwd 2,jnz
fwd 3
jnz
rwd 1
jmp,sub 1,jnz
rwd 1
jmp,sub 1,fwd 2,add 1,rwd 2,jnz
fwd 3
jnz
fwd 1
jmp
jmp,sub 1,fwd 1,add 1,fwd 1,add 1,rwd 2,jnz
fwd 1,sub 1,fwd 1
jmp,sub 1,rwd 2,add 1,fwd 2,jnz
rwd 1
jnz
rwd 2
jmp
jmp
sub 1,fwd 1
jmp,sub 1,fwd 1,add 1,fwd 1,add 1,rwd 2,jnz
fwd 2
jmp,sub 1,rwd 2,add 1,fwd 2,jnz
rwd 3
jnz
fwd 1
jmp,sub 1,jnz
fwd 1
jmp,sub 1,rwd 2,add 1,fwd 2,jnz
rwd 3
jnz
fwd 1
jmp
fwd 1,add 1,rwd 3
jmp,sub 1,fwd 1,add 1,fwd 1,sub 1,rwd 2,jnz
fwd 1
jmp,sub 1,rwd 1,add 1,fwd 1,jnz
fwd 1
jnz
fwd 1
put
Brainfuck equivalent
This Retina script is used to generate the brainfuck equivalent. Note that it only accepts one digit as command argument, and does not check if a command is in the comments.
[->+>>++<<<]>+
[[-<+<+<+>>>]<-<-<[->>>+<<<]>]>>>
[[-<[-<+<+>>]<<[->>+<<]>>>]<[-]<[->>+<<]>>>]>
[[->+>+<<]>->[-<<+>>]<]<<
[[->[->+>+<<]>>[-<<+>>]<<<]>[-]>[-<<+>>]<<<]>
[>+<<<[->+>-<<]>[-<+>]>]>
MATL, 8 bytes
2*GXnGQ/
Explanation
2* % take number n as input and multiply by 2
G % push input again
Xn % compute "2*n choose n"
G % push input again
Q % add 1
/ % divide
R, 35 28 16 bytes
numbers::catalan
Edit: Use numbers package builtin.
𝔼𝕊𝕄𝕚𝕟, 3 chars / 6 bytes
Мƅï
Builtins ftw! So glad I implemented math.js early on.
Bonus solution, 12 chars / 19 bytes
Мơ 2*ï,ï)/⧺ï
Ay! 19th byte!
Evaluates to pseudo-ES6 as:
nchoosek(2*input,input)/(input+1)
Javagony, 223 bytes
public class C{public static int f(int a,int b){try{int z=1/(b-a);}catch(Exception e){return 1;}return a*f(a+1,b);}public static void main(String[]s){int m=Integer.parseInt(s[0])+1;System.out.println(f(m,2*m-1)/f(1,m)/m);}}
Fully expanded:
public class C {
public static int f(int a,int b){
try {
int z=1/(b-a);
} catch (Exception e){
return 1;
}
return a*f(a+1,b);
}
public static void main(String[] s){
int m=Integer.parseInt(s[0])+1;
System.out.println(f(m,2*m-1)/f(1,m)/m);
}
}
pl, 4 bytes
☼ç▲÷
Explanation
In pl, functions take their arguments off the stack and push the result back onto the stack. Normally when there are not enough arguments on the stack, the function simply fails silently. However, something special happens when the amount of arguments on the stack is one off from the arity of the function -- the input variable _ is added to the argument list:
☼ç▲÷
☼ double: takes _ as the argument since there is nothing on the stack
ç combinations: since there is only one item on the stack (and arity is 2), it adds _ to the argument list (combinations(2_,_))
▲ increment last used var (_)
÷ divide: adds _ to the argument list again
In effect, this is the pseudocode:
divide(combinations(double(_),_),_+1);
Ceylon, 60 bytes
Integer c(Integer n)=>(1:n).fold(1)((p,i)=>p*(n+i)/i)/(n+1);
This works up to C30, as Ceylon's Integers are signed 64-bit numbers (C31 has overflow, will be calculated as -4050872099593203).
I don't know if Ceylon has any built-in higher mathematical functions, but then importing the right package would probably longer than just calculating this by foot.
// Catalan number C_n
//
// Question: http://codegolf.stackexchange.com/q/66127/2338
// My answer: http://codegolf.stackexchange.com/a/66425/2338
Integer c(Integer n) =>
// sequence of length n, starting at 1.
(1:n)
// starting with 1, for each element i, multiply the result
// of the previous step by (n+i) and then divide it by i.
.fold(1)((p, i) => p * (n + i) / i)
// divide the result by n+1.
/ (n + 1);
Octave, 22 bytes
@(n)prod(4-6./(2:n+1))
Prolog, 42 bytes
Using recursion is almost always the way to go with Prolog.
Code:
0*1.
N*X:-M is N-1,M*Y,X is(4-6/(N+1))*Y.
Example:
19*X.
X = 1767263190.0
Try it online here
Ruby, 30 bytes
c=->n{n<1?1:c[n-1]*(4+6.0/~n)}
Thanks to xsot, saved few bytes by using complement.
Ungolfed:
c = -> n {
n < 1 ? 1 : c[n-1]*(4+6.0/~n)
}
Usage:
> c=->n{n<1?1:c[n-1]*(4+6.0/~n)}
> c[10]
=> 16796.0
C, 122 121 119 108 bytes
main(j,v)char**v;{long long p=1,i,n=atoi(v[1]);for(j=0,i=n+1;i<2*n;p=(p*++i)/++j);p=n?p/n:p;printf("%d",p);}
I used gcc (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125) to compile in a windows cygwin environment. Input comes in on the command line. It's similar to Sherlock9's Python solution but the loops are combined into one to avoid overflow and get output up to the 20th Catalan number (n=19).
Clojure/ClojureScript, 53 bytes
(defn c[x](if(= 0 x)1(*(c(dec x))(- 4(/ 6(inc x))))))
Clojure can be pretty frustrating to golf in. It's very pithy while still being very readable, but some of the niftier features are really verbose. (inc x) is more idiomatic than (+ x 1) and "feels" more concise, but doesn't actually save characters. And writing chains of operations is nicer as (->> x inc (/ 6) (- 4)), but it's actually longer than just doing it the ugly way.
Jolf, 15 13 bytes
Try it here: ze link. You know what? I almost implemented a built-in. I just didn't have time. Le sigh. Also, there's a bug with my combination code, so I have to implement it with permutations. >_< I'm now also pretty glad I didn't add comments.
//mk*2jjm!jhj
With permutation (9 bytes; invalid, as I fixed this after the challenge was posted):
/mK*2jjhj
With built-in (3 bytes; implemented after challenge :P. I take off my hat to @FlagAsSpam.):
m$j
Jelly, 4 bytes
Ḥc÷‘
How it works
Ḥc÷‘ Left argument: z
Ḥ Compute 2z.
c Hook; apply combinations to 2z and z.
÷‘ Divide the result by z+1.
Matlab, 35 25 bytes
@(n)nchoosek(2*n,n)/(n+1)
Octave, 23 bytes
@(n)nchoosek(2*n,n++)/n
Milky Way 1.5.14, 14 bytes
':2K;*Ny;1+/A!
Explanation
' # read input from the command line
: # duplicate the TOS
2 1 # push integer to the stack
K # push a Pythonic range(0, TOS) as a list
; ; # swap the TOS and the STOS
* # multiply the TOS and STOS
N # push a list of the permutations of the TOS (for lists)
y # push the length of the TOS
+ # add the STOS to the TOS
/ # divide the TOS by the STOS
A # push the integer representation of the TOS
! # output the TOS
or, alternatively, the much more efficient version:
Milky Way 1.5.14, 22 bytes
'1%{;K£1+k1-6;/4+*}A!
Explanation
' # read input from the command line
1 1 1 6 4 # push integer to the stack
%{ £ } # for loop
; ; # swap the TOS and the STOS
K # push a Pythonic range(0, TOS) as a list
+ + # add the TOS and STOS
k # push the negative absolute value of the TOS
- # subtract the STOS from the TOS
/ # divide the TOS by the STOS
* # multiply the TOS and the STOS
A # push the integer representation of the TOS
! # output the TOS
Usage
python3 milkyway.py <path-to-code> -i <input-integer>
Vitsy, 13 Bytes
VV2*FVF/V1+F/
V Capture the input as a final global variable.
V Push it back.
2* Multiply it by 2
F Factorial.
VF Factorial of the input.
/ Divide the second to top by the first.
V1+ 1+input
F Factorial.
/ Divide.
This is a function in Vitsy. How to make it a program that does this, you ask? Concatenate N. c:
Seriously, 9 bytes
,;;u)τ╣E\
Hex Dump:
2c3b3b7529e7b9455c
Explanation:
, Read in evaluated input n
;; Duplicate it twice
u) Increment n and rotate it to bottom of stack
τ╣ Double n, then push 2n-th row of Pascal's triangle
E Look-up nth element of the row, and so push 2nCn
\ Divide it by the n+1 below it.
Python 3, 83 63 62 55 bytes
With thanks to Thomas Kwa.
f=lambda x:x<1or x*f(x-1)
c=lambda n:f(2*n)/f(n)/f(n+1)
f is the factorial function. c returns what is equivalent to 2*n C n divided by n+1.
Mathematica, 16 13 bytes
CatalanNumber
Built-ins, amirite fellas :/
Non-builtin version (21 bytes):
Binomial[2#,#]/(#+1)&
A binomial-less version (25 bytes):
Product[(#+k)/k,{k,2,#}]&
Julia, 23 bytes
n->binomial(2n,n)/(n+1)
This is an anonymous function that accepts an integer and returns a float. It uses the basic binomial formula. To call it, give it a name, e.g. f=n->....
Pyth, 8
/.cyQQhQ
Try it online or run the Test Suite
Explanation
/.cyQQhQ ## implicit: Q = eval(input())
/ hQ ## integer division by (Q + 1)
.c ## nCr
yQ ## use Q * 2 as n
Q ## use Q as r
Japt, 16 bytes
Even Mathematica is shorter. :-/
U*2ª1 o àU l /°U
Ungolfed and explanation
U*2ª 1 o àU l /° U
U*2||1 o àU l /++U
// Implicit: U = input number
U*2||1 // Take U*2. If it is zero, take 1.
o àU // Generate a range of this length, and calculate all combinations of length U.
l /++U // Take the length of the result and divide by (U+1).
// Implicit: output result
Alternate version, based on the recursive formula:
C=_?(4+6/~Z *C$(Z-1):1};$C(U
JavaScript (ES6), 24 bytes
Based on the Python answer.
c=x=>x?(4+6/~x)*c(x-1):1
How it works
c=x=>x?(4+6/~x)*c(x-1):1
c=x=> // Define a function c that takes a parameter x and returns:
x? :1 // If x == 0, 1.
(4+6/~x) // Otherwise, (4 + (6 / (-x - 1)))
*c(x-1) // times the previous item in the sequence.
I think this is the shortest it can get, but suggestions are welcome!
Dyalog APL, 9 bytes
+∘1÷⍨⊢!+⍨
This is a monadic train; it uses the (2x nCr x)/(x+1) formula. Try it online here.
Python 3, 33 bytes
f=lambda n:0**n or(4+6/~n)*f(n-1)
Uses the recurrence
f(0) = 1
f(n) = (4-6/(n+1)) * f(n-1)
The base case of 0 is handled as 0**n or, which stops as 1 when n==0 and otherwise evaluates the recursive expression on the right. The bitwise operator ~n==-n-1 shortens the denominator and saves on parens.
Python 3 is used for its float division. Python 2 could do the same with one more byte to write 6..
Haskell, 27 bytes
g 0=1
g n=(4-6/(n+1))*g(n-1)
A recursive formula. There's got to be a way to save on parens...
Directly taking the product was 2 bytes longer:
g n=product[4-6/i|i<-[2..n+1]]
CJam, 12 bytes
ri_2,*e!,\)/
Beyond input 11, you'll need to tell your Java VM to use more memory. And I wouldn't actually recommend going much beyond 11. In theory, it works for any N though, since CJam uses arbitrary-precision integers.
Explanation
CJam doesn't have a built-in for binomial coefficients, and computing them from three factorials takes a lot of bytes... so we'll have to do something better than that. :)
ri e# Read input and convert it to integer N.
_ e# Duplicate.
2, e# Push [0 1].
* e# Repeat this N times, giving [0 1 0 1 ... 0 1] with N zeros and N ones.
e! e# Compute the _distinct_ permutations of this array.
, e# Get the number of permutations - the binomial. There happen to be 2n-over-n of
e# of them. (Since 2n-over-n is the number of ways to choose n elements out of 2n, and
e# and here we're choosing n positions in a 2n-element array to place the zeros in.)
\ e# Swap with N.
)/ e# Increment and divide the binomial coefficient by N+1.
TI-BASIC, 11 bytes
(2Ans) nCr Ans/(Ans+1
Strangely, nCr has higher precedence than multiplication.
