2
\$\begingroup\$

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.

\$\endgroup\$

marked as duplicate by Peter Taylor code-golf Jul 3 '17 at 22:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

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
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 7 5 bytes

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

;îxsc

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

Python 2, 88 81 80 79 74 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!

\$\endgroup\$
  • \$\begingroup\$ 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). \$\endgroup\$ – Jonathan Allan Jul 3 '17 at 12:13
  • \$\begingroup\$ ...and two more bytes removing the line k=n/2 and using the code I put in the last comment for the last line. \$\endgroup\$ – Jonathan Allan Jul 3 '17 at 18:34
1
\$\begingroup\$

Python 2, 40 38 bytes

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

Try it online!

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

Jelly, 8 bytes

c:2$×Ḃ‘$

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.