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 results after we're done
4Å1 # Start this recursive list with [1,1,1,1], thus a(0)=a(1)=a(2)=a(3)=1
# Within the recursive environment, do the following:
λ # Push the list of values in the range [a(0),a(n)]
¨ # Remove the last one to make the range [a(0),a(n-1)]
 # 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-3),a(0)]
s # Swap to get the [a(0),a(n-1)] 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-1)]
* # Multiply the values at the same indices in both lists,
# so we'll have a list with the values [a(n-3)*a(2),...,a(0)*a(n-1)]
O # Take the sum of this list
₁ + # And add it to the a(n-1)'th value
# (afterwards the resulting list is output implicitly)
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:
- (0-based) indexing 1λèλ1šÂ¨¨¨øPO
: Try it online;
- Infinite list λλ1šÂ¨¨¨øPO
: Try it online. (Note that 2 bytes are saved here instead of 1, because the recursive environment starts with \$a(0)=1\$ by default.)
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 results after we're done
1 # Start this recursive list with 1, thus a(0)=1
# Within the recursive environment, do the following:
λ # Push the list of values in the range [a(0),a(n)]
1š # Prepend 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
# (afterwards the resulting list is output implicitly)
Correct me if I'm wrong here, but the modification for a one-indexed formula is to change
a(n-1-k)
toa(n-k)
, correct? – Sumner18 – 2019-09-19T14:08:37.2239
This reminds me of The strong law of small numbers
– Luis Mendo – 2019-09-19T14:13:31.340