Calculate the middle number of a row of the pascal triangle [duplicate]

This question already has an answer here:

Write a function that calculates a row of the pascal triangle and returns the middle number, if there are 2 it should return their sum.

Example:

5

would return

20

Example 2:

4

would return

6

EDIT:

To be clear:

a) if the number inputted is odd then find then return the middle number of a row on the pascal triangle.

b) if the number inputted is even then find the two middle numbers of the row on the pascal triangle and sum the 2 numbers.

The nth row is using zero-based indicies. For numbers other than zero, this is the row which has its second number as the number inputted into the function.

marked as duplicate by Peter Taylor code-golf StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Jul 3 '17 at 22:17

• So nC(n/2) if n is even and 2(nC((n-1)/2)) if n is odd? – Leaky Nun Jul 3 '17 at 10:25
• – Charlie Jul 3 '17 at 10:26
• Which built-in function may we not use? – KSFT Jul 3 '17 at 10:29
• @KSFT any, you can't actually write any code – Skidsdev Jul 3 '17 at 10:30
• I don't see why this was closed; the OPs sequence is not the Catalan numbers. – Chas Brown Jul 3 '17 at 22:32

Ohm, 6 bytes

½⌠Ddac

Try it online!

If the number is even it is you calculate (n n/2) if it is odd (n+1 (n+1)/2)

Inplicit input
½        Half
⌠       Ceil
D      Duplicate on stack
d     x2
a    Swap on stack
c   Binomial

05AB1E, 7 5 bytes

Saved 2 bytes using the duplicate/swap technique from FrodCube's Ohm answer

;îxsc

Try it online!

Jelly, 5 bytes

HĊµḤc

A monadic link, taking a (0-indexed) row and returning the middle number or the sum of the two middle numbers - equivalently the middle number of the row beneath).

Try it online!, or see the first 20 values in the test suite.

How?

HĊµḤc - Link: number v
H     - halve = v/2
Ċ    - ceiling, i.e. (v + isOdd?(v)) / 2
µ   - monadic chain separation, call that k
Ḥ  - double k (this is n: v if v was even, or v+1 if v was odd)
c - that choose k = nCk, the required result

R, 35 bytes

function(n)(1+n%%2)*choose(n,n%/%2)

An anonymous function.

Since odd rows need to be doubled, n%%2+1 is 2 when n is odd and 1 when nn is even. Then, I multiply by the appropriate binomial coefficient.

Try it online!

Python 2, 8881807974 72 bytes

Thanks to @JonathanAllen

import math
f=math.factorial
n=input()
print(n%2+1)*f(n)/f(n/2)/f(n-n/2)

Try it online!

• No need for the int (saving 5), since division is integer division by default in Python 2, which means you can remove the assignment to k too and save another 2 with print(n%2+1)*f(n)/f(n/2)/f(n-n/2). – Jonathan Allan Jul 3 '17 at 12:13
• ...and two more bytes removing the line k=n/2 and using the code I put in the last comment for the last line. – Jonathan Allan Jul 3 '17 at 18:34

Python 2, 40 38 bytes

f=lambda n:n<1or(-n|1)*2*f(n-2)/(-n/2)

Try it online!

• (4+2./(-n/2))*f(n-2) saves a few bytes. – xnor Jul 3 '17 at 23:52

Jelly, 8 bytes

c:2$×Ḃ‘$

Try it online!

Dyalog APL, 12 bytes

{k!2×k←⌈⍵÷2}

or

(⊢!2∘×)∘⌈÷∘2

Try it online!

How?

⍵÷2 - divide by 2

- ceiling

k← - assign to k

- double

k! - binomial with k