g | x | w | all
Bytes Lang Time Link
063TI83 BASIC240509T160257Zmadeforl
064Casio BASIC Casio fx9750GIII240823T164928Zmadeforl
025Scratch 3.0250105T172126Zmadeforl
595jbasher2241213T174724Zmadeforl
072PowerShell Core241115T075114ZJulian
01305AB1E190920T075315ZKevin Cr
072Scala 3240824T093004Z138 Aspe
042Arturo240824T081705Zchunes
021HOPS240510T070834Zalephalp
102Google Sheets240509T231146Zz..
072YASEPL240404T155115Zmadeforl
023Pip240404T003504ZDLosc
033Wolfram Language Mathematica190919T203438Zatt
073Octave190920T121931ZSanchise
067C/C++190921T105116Zpolfosol
041Ruby190920T062402ZG B
030Pyth190921T111600Zar4093
026Charcoal190921T101905ZNeil
081Forth gforth190920T143951Zreffu
039Haskell190919T173346Znimi
016Japt190919T170831ZShaggy
061Perl 5 MListUtil=sum190919T175546ZXcali
042JavaScript ES6190919T124158ZArnauld
051Python190919T141418Zxnor
01105AB1E190919T135310ZGrimmy
034APL Dyalog Extended190919T124049ZAdá
065Haskell190919T133635ZJo King
076Haskell190919T132041Zflawr
059Python 3190919T131200Zovs
044Perl 6190919T131131ZJo King
017Jelly190919T125148ZNick Ken

TI-83 BASIC, 86 63 bytes

displays the first Nth numbers as a list

Input M
randInt(1,1,M+1→L₁
For(N,4,M
L₁(N)+sum(seq(L₁(K)L₁(N-K),K,1,N-3→L₁(N+1
End
L₁

I'd say this is pretty accurate

Casio BASIC (Casio fx-9750GIII), 76 68 64 bytes

displays the first N+1 numbers

?→M
Not Not RanList#(M→List1
For 4→N To M
List1[N]+Σ(List1[K]List1[N-K],K,1,N-3→List1[N+1
Next
List1

displays the list of numbers at the end

translation of my TI-84 answer

Scratch 3.0, 25 blocks

just clarifying: this is a non-competing answer

Scratch code.

check it out in action here

jbasher2, 595 bytes

create a with type list
push 1 to a
push 1 to that
push 1 to that
push 1 to that
set that to a
create k with type number
create i with type number
set 0 to i
create l with type number
ask for input
set that to l
create v with type number
create o with type number
while i < l
get item from a at i
set that to v
set 2 to k
while k < i
set 0 to o
subtract i by 1
subtract that by k
get item from a at that
set that to o
get item from a at k
multiply that by o
add that by v
set that to v
add 1 by k
set that to k
endwhile
output v
add 1 by i
set that to i
change item in a at index i to v
endwhile

(compressed, 248 bytes)

Ca @#listLD1 ^aLD1 ^thatLD1 ^thatLD1 ^thatL%th;^aLCk @#PLCi @#PL%0 ^iLCl @#PL[]\L%th;^lLCv @#PLCo @#PLQi < lL{}|a ;iL%th;^vL%2 ^kLQk < iL%0 ^oL(i *1L(th;*kL{}|a ;thatL%th;^oL{}|a ;kL],th;*oL&th;*vL%th;^vL&1 *kL%th;^kLWLUvL&1 *iL%th;^iLF}Ga ;Hi ^vLW

this is SUPER self explanatory. prints the first N numbers

PowerShell Core, 72 bytes

filter f{&({$s=0
3..--$n|%{$s+=($_|f)*($n-$_|f)}
$s},{1})[+($n=$_)-lt4]}

Try it online!

Implementation of the recursive definition as described in DLosc's answer, one-indexed.

05AB1E, 17 16 13 bytes

4Å1λ£λ¨Â¦¦s¦¦*O+

Not shorter than the existing 05AB1E answer, but I wanted to try the recursive functionality of the new 05AB1E version as practice for myself. Could perhaps be golfed by a few bytes. EDIT: And it indeed can, see the recursive version of @Grimy's 05AB1E answer below, which is 13 bytes.

Outputs the first \$n\$ items: Try it online.

Could be changed to the 0-based \$n\$'th item when replacing £ with è: Try it online;
or an infinite list by removing £: Try it online.

Explanation:

This implements the formula used in the challenge description like this:
\$a(n)= a(n-1) + \sum_{k=2}^{n-1}(a(k)\cdot a(n-1-k))\$

\$a(0)=a(1)=a(2)=a(3)=1\$

   λ              # Create a recursive environment,
    £             # to output the first (implicit) input amount of terms
                  # (which will be output implicitly afterwards)
4Å1               # Start with a(0)=a(1)=a(2)=a(3)=1
                  # Where every following a(n) is calculated as:
                  #  (implicitly push the previous term a(n-1))
     λ            #  Push the list of previous terms in the range [a(0),a(n-1)]
      ¨           #  Remove the last one to make the range [a(0),a(n-2)]
       Â          #  Bifurcate this list (short for Duplicate & Reverse copy)
        ¦¦        #  Remove the first two items of the reversed list,
                  #  so we'll have a list with the values in the range [a(n-4),a(0)]
          s       #  Swap to get the [a(0),a(n-2)] list again
           ¦¦     #  Remove the first two items of this list as well,
                  #  so we'll have a list with the values in the range [a(2),a(n-2)]
             *    #  Multiply the values at the same indices in both lists,
                  #  so we'll have a list with the values [a(n-4)*a(2),...,a(0)*a(n-2)]
              O   #  Take the sum of this list
               +  #  And add it to the implicit a(n-1)'th value

13 bytes version of @Grimy (make sure to upvote his answer if you haven't yet!):

1λ£λ1šÂ¨¨¨øPO

Outputs the first \$n\$ items: Try it online.

Can again be changed to 0-based indexing or an infinite list instead:

Explanation:

This instead implements the formula found by @xnor for his Python answer like this:
\$a(n) = \sum_{k=2}^{n-1}(a(k)\cdot a(n-2-k))\$

\$a(-1) = a(0) = a(1) = a(2) = 1\$

 λ             # Create a recursive environment,
  £            # to output the first (implicit) input amount of terms
               # (which will be output implicitly afterwards)
1              # Start with a(0)=1
               # Where every following a(n) is calculated as:
   λ           #  Push the list of terms in the range [a(0),a(n-1)]
    1š         #  Prepend a 1 in front of this list
      Â        #  Bifurcate the list (short for Duplicate & Reverse copy)
       ¨¨¨     #  Remove (up to) the last three value in this reversed list
          ø    #  Create pairs with the list we bifurcated earlier
               #  (which will automatically remove any trailing items of the longer list)
           P   #  Get the product of each pair (which will result in 1 for an empty list)
            O  #  And sum the entire list

Scala 3, 72 bytes

n=>n match{case -1|0|1|2=>1 case _=>(2to n-1).map(k=>a(k)*a(n-2-k)).sum}

Attempt This Online!

Arturo, 44 42 bytes

f:$->n[?n<4->1->∑map..4n'i->*f i-1f n-i]

Try it!

Port of G B's Ruby answer.

HOPS, 21 bytes

A=1+x*A*(1-x-x^2+x*A)

Attempt This Online!

Based on the generating function on OEIS.

Google Sheets, 102 bytes

Returns the \$nth\$ number where \$n\$ is in A1.

=LET(F,LAMBDA(F,n,IF(n<4,1,F(F,n-1)+SUM(MAP(SEQUENCE(n-3,1,2),LAMBDA(k,F(F,k)*F(F,n-2-k)))))),F(F,A1))

YASEPL, 77 73* 72 bytes

outputs each number (seperated by new line), asks for how many numbers with a prompt

=n$3=b'£1©1©1©1©1=a$`1=k$+`2!m¥k,1!o$n--k¥o,1*m!a+o!k+}2,n,2>a!1©a!+}2,b

explanation soon

* i made a mistake with the bytecount, i thought it was 44

Pip, 23 bytes

Y1RL5LayPB$+:y*RyH-3y@a

Uses 1-indexing. Attempt This Online!

Explanation

The program uses 1-indexing because it actually computes the following 0-indexed sequence:

1, 1, 1, 1, 1, 2, 4, 8, 16, 33, ...

i.e. the same thing with an extra 1 on front. This makes the formula simpler:

$$a(n)= \sum_{k=3}^{n-1} a(k) \cdot a(n-1-k)$$

Y1RL5LayPB$+:y*RyH-3y@a
 1RL5                    ; Make a list of five 1s
Y                        ; Yank that into y
     La                  ; Loop (program argument) times:
             y           ;   Take the current list y
              *Ry        ;   Multiply it itemwise by its reverse
                 H-3     ;   Take all but the last three elements
          $+:            ;   Sum
       yPB               ;   Push that value onto the end of y
                    y@a  ; Get the item in y at index (program argument)

Wolfram Language (Mathematica), 36 33 bytes

Sum[#0[#-i]#0@--i,{i,4,#}]~Max~1&

Try it online!

1-indexed.

The 2-indexed sequence is 2 bytes shorter: Sum[#0@i#0[#-i],{i,#-4}]~Max~1&. Try it online!

Octave, 73 bytes

g=(1:4).^0;for(i=3:(n=input('')))g(i+2)=g(4:i+1)*g(i-(2:i-1))';end;g(end)

Try it online!

-2 bytes thanks to Stewie Griffin. Once more, the imperative approach wins over the functional recursive approach. That one is shown below.

Octave, 75 bytes

f(f=@(a)@(n){@()sum(arrayfun(@(k)a(a)(k)*a(a)(n-2-k),2:n-1)),1}{2-(n>3)}())

Try it online!

Captcha wanted to verify I was a human when posting this. To be honest, I'm not so sure.

C/C++, 70 69 67 bytes

-1 bytes thanks to Jonathan.

int a(int n){int k=2,s=0;while(++k<n)s+=a(k)*a(n+~k);return s?s:1;}

Try it online!

Ruby, 42 41 bytes

f=->n{n<4?1:(4..n).sum{|i|f[i-1]*f[n-i]}}

Try it online!

1-indexed (to save 1 byte)

Pyth, 30 bytes

J*4]1VQ=+J+eJsPP*M.t,PJ_PJ0;<J

Try it online!

Returns the first \$n\$ elements of the sequence.

J*4]1VQ=+J+eJsPP*M.t,PJ_PJ0;<JQ # Full program, last Q = input (implicitly added)
J*4]1                  # J = 4 * [1] (=[1,1,1,1])
VQ                     # for N in range(Q):
  =+J                  #  J +=
     +eJ               #   J[-1] + 
        s              #    sum(                           )
           *M          #     map(__operator_mul,          )
             .t      0 #      transpose(          , pad=0)
               ,       #       [       ,         ]
                PJ     #         J[:-1] 
                  _PJ  #                 J[1::-1]
<JQ                    # J[::Q]

Alternative: Replace < with @ to return the \$n\$-th element of the sequence, 0-indexed.

Charcoal, 26 bytes

F⁵⊞υ¹FN⊞υΣ✂E⮌υ×κ§υλ³→I§υ±⁴

Try it online! Link is to verbose version of code. Prints the 0-indexed nth number, although it calculates using 1-indexing internally. Explanation:

F⁵⊞υ¹

Start with a[0] = a[1] = a[2] = a[3] = a[4] = 1. Yes, this is 1-indexed, but then with an extra zeroth value. That's code golf for you.

FN

Calculate an additional n terms. This is overkill, but it makes finding the desired term easier when n<5.

⊞υΣ✂E⮌υ×κ§υλ³

For each term, compute the next term as the sum of the terms so far termwise multiplied by the reverse of the terms so far, excluding three terms.

This is a no-op used to trick Charcoal into parsing the 2-argument form of Slice, otherwise I would have to use a less golfy way of removing three terms.

I§υ±⁴

Output the 4th last term.

Forth (gforth), 99 81 bytes

: f recursive dup 4 > if 0 over 3 do over 1- i - f i f * + loop else 1 then nip ;

Try it online!

Output is nth term and input is 1-indexed

Edit: Saved 17 bytes by switching to xnor's formula. Saved another 1 byte by using 1-indexed

Code Explanation

: f                     \ start a new word definition
  recursive             \ mark that this word will be recursive
  dup 4 >               \ duplicate the input and check if it is greater than 4
  if                    \ if it is:
    0 over              \ create an accumulator and copy n to top of stack
    3 do                \ start counted loop from 3 to n-1
      over 1- i - f     \ recursively calculate f(n-1-i)
      i f               \ recursively calculate f(i)
      * +               \ multiply results and add to accumulator
    loop                \ end the counted loop        
  else                  \ otherwise, if n < 5
    1                   \ put 1 on the stack
  then                  \ end the if block
  nip                   \ drop n from the stack
;                       \ end the word definition

Haskell, 49 43 39 bytes

a n=max(sum[a k*a(n-2-k)|k<-[2..n-1]])1              

Try it online!

For n<3 the sum is 0, so max ... 1 raises it to 1.

Edit: -6 bytes thanks to @Jo King.

Japt, 19 17 16 bytes

Outputs the nth term, 1-indexed.

@Zí*Zz2)Ťx}g4Æ1

Try it

@Zí*Zz2)Ťx}g4Æ1     :Implicit input of integer U
@                    :Function taking an array as an argument via parameter Z
 Zí                  :  Interleave Z with
    Zz2              :  Z rotated clockwise by 180 degrees (simply reversing would be a bye shorter but would modify the original array)
   *                 :  Reduce each pair by multiplcation
       )             :  End interleave
        Å            :  Slice off the first element
         ¤           :  Slice off the first 2 elements
          x          :  Reduce by addition
           }         :End function
            g        :Pass the following as Z, push the result back to it and repeat until it has length U
             4Æ1     :Map the range [0,4) to 1s
                     :Implicit output of the last element

Perl 5 -MList::Util=sum, 61 bytes

sub a{my$b=-2+pop;$b<2||sum a($b+1),map{a($_)*a($b-$_)}2..$b}

Try it online!

JavaScript (ES6), 42 bytes

A port of xnor's solution.

0-indexed.

f=(n,k=2)=>n<3||k<n&&f(k)*f(n+~++k)+f(n,k)

Try it online!


JavaScript (ES6),  83  75 bytes

A faster, less recursive, but significantly longer solution.

0-indexed.

f=(n,i,a=[p=1])=>a[n]||f(n,-~i,[...a,p+=(h=k=>k<i&&a[k]*a[i-++k]+h(k))(2)])

Try it online!

Python, 51 bytes

f=lambda n,k=2:n<3or k<n and f(k)*f(n-k-2)+f(n,k+1)

Try it online!

Simplifies the formula a bit:

$$a(n) = \sum_{k=2}^{n-1} a(k)\cdot a(n-2-k)$$

$$ a(-1) = a(0)= a(1)= a(2) = 1$$

05AB1E, 14 13 11 bytes

$ƒˆ¯Âø¨¨¨PO

Try it online!

Outputs the nth element, 0-indexed.

$                # push 1 and the input
 ƒ               # repeat (input+1) times
  ˆ              #  add the top of the stack (initially 1) to the global array
   ¯             #  push the global array
    Â            #  and a reversed copy of it
     ø           #  zip the two together, giving a list of pairs
      ¨¨¨        #  drop the last 3 pairs
         P       #  take the product of each pair (or 1 if the list is empty)
          O      #  take the sum of those products
                 #  after the last iteration, this is implicitly output;
                 #  otherwise, it's added to the global array by the next iteration

APL (Dyalog Extended), 34 bytesSBCS

-2 thanks to dzaima.

Anonymous prefix lambda.

{⍵≤3:1⋄+/(∇⍵-1),⍵(-×⍥∇¯2+⊢)¨4…⍵}

Try it online!

Haskell, 65 bytes

f a|a<4=1|z<-g[2..a]=sum$zipWith(*)z$reverse(1:g[0..a-4])
g=map f

Try it online!

You can use either f to get a single element of a sequence, or pass a list of values to g and get all the indexes for that list.

Haskell, 76 bytes

1:1:1:f[1,1,1]
f(x:z)|y<-x+sum(zipWith(*)(init$init z)$reverse z)=y:f(y:x:z)

Try it online!

Python 3, 59 bytes

really inefficient, a(13) doesn't finish on TIO.

a=lambda n,k=2:n<4or(n-k<2)*a(n-1)or a(k)*a(n-k-2)+a(n,k+1)

Try it online!

Perl 6, 44 bytes

{1,1,1,1,{sum @_[2..*]Z*@_[@_-4...0,0]}...*}

Try it online!

Anonymous code block that returns a lazy infinite sequence of values. This pretty much implements the sequence as described, with the shortcut that it zip multiplies all the elements so far after the second element with the reverse of the list starting from the fourth element and adding an extra 1 at the end.

Explanation:

{                                          }  # Anonymous code block
                                       ...*   # Create an infinite sequence
 1,1,1,1,                                     # Starting with four 1s
         {                            }       # Where each new element is:
          sum                                   # The sum of
              @_[2..*]                          # The second element onwards
                      Z*                        # Zip multiplied with
                        @_[@_-4...0  ]          # The fourth last element backwards
                                   ,0           # And 1

Jelly, 17 bytes

1WṪ;+¥×Uṫ3SƲ;@Ʋ⁸¡

Try it online!

A monadic link taking the zero-indexed \$n\$ and returning the list of generalized Catalan numbers from \$0\$ to \$n\$.