Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Commonmark migration
Source Link

MATL, 16 bytes

tZv=Gq:"t5BZ+]vs

Try it online!

###Explanation

Explanation

This repeatedly applies convolution to generate the rows. For example, for input n=5 we start with the first row

0 0 0 0 1 0 0 0 0

Convolving with [1 0 1] gives

0 0 0 1 0 1 0 0 0

Repeating the operation gives

0 0 1 0 2 0 1 0 0

then

0 1 0 3 0 3 0 1 0

etc. Concatenating these arrays vertically and computing the sum of each column gives the result.

t % Input n implictly. Duplicate
Zv % Symmetric range. Gives [1 2 3 4 5 4 3 2 1] for input 5
= % Equal to (element-wise). Gives [0 0 0 0 1 0 0 0 0]. This is the first row
Gq: % Push [1 2 ... n-1]
" % For each. This executes the following code n-1 times
 t % Duplicate
 5B % Push 5 in binary, that is, [1 0 1]
 Z+ % Convolution keeping size
] % End
v % Concatenate all results vertically 
s % Sum. Display implicitly.

MATL, 16 bytes

tZv=Gq:"t5BZ+]vs

Try it online!

###Explanation

This repeatedly applies convolution to generate the rows. For example, for input n=5 we start with the first row

0 0 0 0 1 0 0 0 0

Convolving with [1 0 1] gives

0 0 0 1 0 1 0 0 0

Repeating the operation gives

0 0 1 0 2 0 1 0 0

then

0 1 0 3 0 3 0 1 0

etc. Concatenating these arrays vertically and computing the sum of each column gives the result.

t % Input n implictly. Duplicate
Zv % Symmetric range. Gives [1 2 3 4 5 4 3 2 1] for input 5
= % Equal to (element-wise). Gives [0 0 0 0 1 0 0 0 0]. This is the first row
Gq: % Push [1 2 ... n-1]
" % For each. This executes the following code n-1 times
 t % Duplicate
 5B % Push 5 in binary, that is, [1 0 1]
 Z+ % Convolution keeping size
] % End
v % Concatenate all results vertically 
s % Sum. Display implicitly.

MATL, 16 bytes

tZv=Gq:"t5BZ+]vs

Try it online!

Explanation

This repeatedly applies convolution to generate the rows. For example, for input n=5 we start with the first row

0 0 0 0 1 0 0 0 0

Convolving with [1 0 1] gives

0 0 0 1 0 1 0 0 0

Repeating the operation gives

0 0 1 0 2 0 1 0 0

then

0 1 0 3 0 3 0 1 0

etc. Concatenating these arrays vertically and computing the sum of each column gives the result.

t % Input n implictly. Duplicate
Zv % Symmetric range. Gives [1 2 3 4 5 4 3 2 1] for input 5
= % Equal to (element-wise). Gives [0 0 0 0 1 0 0 0 0]. This is the first row
Gq: % Push [1 2 ... n-1]
" % For each. This executes the following code n-1 times
 t % Duplicate
 5B % Push 5 in binary, that is, [1 0 1]
 Z+ % Convolution keeping size
] % End
v % Concatenate all results vertically 
s % Sum. Display implicitly.
added 94 characters in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

MATL, 16 bytes

tZv=Gq:"t5BZ+]vs

Try it online!

###Explanation

This repeatedly applies convolution to generate the rows. For example, for input n=5 we start with the first row

0 0 0 0 1 0 0 0 0

Convolving with [1 0 1] gives

0 0 0 1 0 1 0 0 0

Repeating the operation gives

0 0 1 0 2 0 1 0 0

then

0 1 0 3 0 3 0 1 0

etc. Concatenating these arrays vertically and computing the sum of each column gives the result.

t % Input n implictly. Duplicate
Zv % Symmetric range. Gives [1 2 3 4 5 4 3 2 1] for input 5
= % Equal to (element-wise). Gives [0 0 0 0 1 0 0 0 0]. This is the first row
Gq: % Push [1 2 ... n-1]
" % For each. This executes the following code n-1 times
 t % Duplicate
 5B % Push 5 in binary, that is, [1 0 1]
 Z+ % Convolution keeping size
] % End
v % Concatenate all results vertically 
s % Sum. Display implicitly.

MATL, 16 bytes

tZv=Gq:"t5BZ+]vs

Try it online!

###Explanation

This repeatedly applies convolution to generate the rows. For example, for input n=5 we start with the first row

0 0 0 0 1 0 0 0 0

Convolving with [1 0 1] gives

0 0 0 1 0 1 0 0 0

Repeating the operation gives

0 0 1 0 2 0 1 0 0

then

0 1 0 3 0 3 0 1 0

etc.

t % Input n implictly. Duplicate
Zv % Symmetric range. Gives [1 2 3 4 5 4 3 2 1] for input 5
= % Equal to (element-wise). Gives [0 0 0 0 1 0 0 0 0]. This is the first row
Gq: % Push [1 2 ... n-1]
" % For each. This executes the following code n-1 times
 t % Duplicate
 5B % Push 5 in binary, that is, [1 0 1]
 Z+ % Convolution keeping size
] % End
v % Concatenate all results vertically 
s % Sum. Display implicitly.

MATL, 16 bytes

tZv=Gq:"t5BZ+]vs

Try it online!

###Explanation

This repeatedly applies convolution to generate the rows. For example, for input n=5 we start with the first row

0 0 0 0 1 0 0 0 0

Convolving with [1 0 1] gives

0 0 0 1 0 1 0 0 0

Repeating the operation gives

0 0 1 0 2 0 1 0 0

then

0 1 0 3 0 3 0 1 0

etc. Concatenating these arrays vertically and computing the sum of each column gives the result.

t % Input n implictly. Duplicate
Zv % Symmetric range. Gives [1 2 3 4 5 4 3 2 1] for input 5
= % Equal to (element-wise). Gives [0 0 0 0 1 0 0 0 0]. This is the first row
Gq: % Push [1 2 ... n-1]
" % For each. This executes the following code n-1 times
 t % Duplicate
 5B % Push 5 in binary, that is, [1 0 1]
 Z+ % Convolution keeping size
] % End
v % Concatenate all results vertically 
s % Sum. Display implicitly.
added 843 characters in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

MATL, 16 bytes

tZv=Gq:"t5BZ+]vs

Try it online!

###Explanation

This repeatedly applies convolution to generate the rows. For example, for input n=5 we start with the first row

0 0 0 0 1 0 0 0 0

Convolving with [1 0 1] gives

0 0 0 1 0 1 0 0 0

Repeating the operation gives

0 0 1 0 2 0 1 0 0

then

0 1 0 3 0 3 0 1 0

etc.

t % Input n implictly. Duplicate
Zv % Symmetric range. Gives [1 2 3 4 5 4 3 2 1] for input 5
= % Equal to (element-wise). Gives [0 0 0 0 1 0 0 0 0]. This is the first row
Gq: % Push [1 2 ... n-1]
" % For each. This executes the following code n-1 times
 t % Duplicate
 5B % Push 5 in binary, that is, [1 0 1]
 Z+ % Convolution keeping size
] % End
v % Concatenate all results vertically 
s % Sum. Display implicitly.

MATL, 16 bytes

tZv=Gq:"t5BZ+]vs

Try it online!

MATL, 16 bytes

tZv=Gq:"t5BZ+]vs

Try it online!

###Explanation

This repeatedly applies convolution to generate the rows. For example, for input n=5 we start with the first row

0 0 0 0 1 0 0 0 0

Convolving with [1 0 1] gives

0 0 0 1 0 1 0 0 0

Repeating the operation gives

0 0 1 0 2 0 1 0 0

then

0 1 0 3 0 3 0 1 0

etc.

t % Input n implictly. Duplicate
Zv % Symmetric range. Gives [1 2 3 4 5 4 3 2 1] for input 5
= % Equal to (element-wise). Gives [0 0 0 0 1 0 0 0 0]. This is the first row
Gq: % Push [1 2 ... n-1]
" % For each. This executes the following code n-1 times
 t % Duplicate
 5B % Push 5 in binary, that is, [1 0 1]
 Z+ % Convolution keeping size
] % End
v % Concatenate all results vertically 
s % Sum. Display implicitly.
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382
Loading

AltStyle によって変換されたページ (->オリジナル) /