# Is it a row of Pascal's triangle?

Pascal's triangle is a triangular diagram where the values of two numbers added together produce the one below them.

This is the start of it:

    1
1 1
1 2 1
1 3 3 1
1 4 6 4 1


You can see that the outside is all 1s, and each number is the sum of the two above it. This continues forever.

Your challenge is to check whether a non-empty array of positive integers is a row of Pascal's triangle, somewhere down the line.

You should output two distinct values for truthy and falsy.

## Scoring

This is , shortest wins.

## Testcases

Truthy:

[1]
[1,2,1]
[1,3,3,1]
[1,5,10,10,5,1]
[1,6,15,20,15,6,1]
[1,7,21,35,35,21,7,1]


Falsy:

[2]
[1,2]
[1,1,2]
[2,2,2,2]
[1,2,10,2,1]
[1,2,3,4,5,6]
[1,3,5,10,5,3,1]

• Can we take the length of the input as input as well? Sep 15 at 20:09
• Darn you for preventing sum of n = 2^n. 😂 Sep 15 at 21:06
• @DLosc No, you don't. Sep 20 at 3:59

# Jelly, 6 bytes

L’cŻ$⁼  Try it online! As each row of Pascal's triangle has a unique length $$\n\$$, all we have to do is reconstruct the row, given its length, and check if it equals the original input. As each row is given by $$\\binom{n-1}{i}, 0 \le i < n\$$, we just calculate that (as Jelly has a 1 byte for binomial coefficient) In fact, if we can take the length of the input as a second input, we can get 5 bytes ## How it works L’cŻ$⁼ - Main link. Takes a list L on the left
L      - Length of L
’     - Decrement
$- Last 2 links as a monad f(len-1): Ż - Range from 0 to len-1 c - Binomial coefficient of len-1 and each element in the range ⁼ - Does that equal the input?  # Haskell, 3631 26 bytes f[1]=1 f(1:b)=f$scanr1(-)b


Try it online!

This version errors as a false indicator and returns 1 as the true indicator. It's short but I generally find these sorts of answers a little cheaty so below I have a version with a more traditional output method:

# 4237 32 bytes

f[1]=1

# Pari/GP, 20 bytes

a->a==binomial(#a-1)


When binomial takes only one argument, it returns the n'th row of the Pascal's triangle.

Try it online!

# Ruby, 51 bytes

->l{l.all?{r=0;*l,r=l.map{|x|r=x-r};r==(l[0]?0:1)}}


Try it online!

# Pascal (FPC), 165 bytes

function f(a:Array of Integer):integer;var i,n,c:Integer;begin
i:=1;c:=1;f:=1;n:=High(a)+1;for i:=1 to n do begin
if a[i-1]<>c then f:=0;c:=Trunc(c*(n-i)/i);end;end;


Try it online!

# Pip-x, 20 14 bytes

L#al+:lPE!ll=a


Try it here! TIO doesn't support the -x flag, but you can simulate it using the header: Try it online!

### Explanation

We generate the nth row of Pascal's triangle, where n is the length of the input list; then we output truthy if the row and the input are equal, falsey otherwise. Heavily based on my answer to Generate Pascal's triangle, which I recommend you read for a better explanation of the core algorithm.

                a is 1st cmdline arg, evaluated (-x flag); l is empty list
L               Loop
#a              len(a) times:
!l       1 if l is empty, 0 otherwise
lPE         l with that value prepended
l+:            Add to l itemwise and assign back to l
l=a  1 if l equals a, 0 otherwise


# Charcoal, 20 bytes

⬤θ⎇κ⁼×⁻Ｌθκ§θ⊖κ×κι⁼¹ι


Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. - if it's a row, nothing if not. Explanation: The first element must always be 1. Subsequent elements obey the recurrence relation that the previous element multiplied by the inclusive number of elements remaining equals the current element multiplied by its index.

 θ                      Input array
⬤                       Do all elements satisfy
⎇κ                    If not first element then
§θ⊖κ          Previous element
×                  Multiplied by
⁻Ｌθκ              Number of elements remaining
⁼                   Equals
×κι       Current element multiplied by its index
⁼¹ι    If first element then it is 1
Implicitly print
`