Skip to main content
added 1 character in body
Source Link
Wheat Wizard
  • 100k
  • 22
  • 282
  • 661

Haskell, 46 bytes

g _ 0=[]
g(h:t)n=h:g(t++[sum$h:t])(n-1)
g.g[1]

Attempt This Online!

This is based on xnor's answer but it employs 1 clever trick.

The \$n\$-bonacci sequence always start with \$n\$ 1s. A more general version of this might start with just any \$n\$ values. And this more general version is what we implement with g. g takes a list of \$n\$ integers and a value \$m\$ and gives us a list of the first \$m\$ terms of this generalized \$n\$-bonacci sequence.

The trick is then that g[1] is a cheap way to generate \$n\$ 1s. Since the sequence starting with [1] is just an endless stream of 1s. So we use g in two ways, and even though g might be slightly longer because it implements something a little more general, it saves bytes because it serves two purposes.

Haskell, 46 bytes

g _ 0=[]
g(h:t)n=h:g(t++[sum$h:t])(n-1)
g.g[1]

Attempt This Online!

This is based on xnor's answer but it employs 1 clever trick.

The \$n\$-bonacci sequence always start with \$n\$ 1s. A more general version of this might start with just any \$n\$ values. And this more general version is what we implement with g. g takes a list of \$n\$ integers and a value \$m\$ and gives us a list of the first \$m\$ terms of this generalized \$n\$-bonacci sequence.

The trick is then that g[1] is a cheap way to generate \$n\$ 1s. Since the sequence starting with [1] is just an endless stream of 1s. So we use g in two ways, and even though g might be slightly longer because it implements something a little more general it saves bytes because it serves two purposes.

Haskell, 46 bytes

g _ 0=[]
g(h:t)n=h:g(t++[sum$h:t])(n-1)
g.g[1]

Attempt This Online!

This is based on xnor's answer but it employs 1 clever trick.

The \$n\$-bonacci sequence always start with \$n\$ 1s. A more general version of this might start with just any \$n\$ values. And this more general version is what we implement with g. g takes a list of \$n\$ integers and a value \$m\$ and gives us a list of the first \$m\$ terms of this generalized \$n\$-bonacci sequence.

The trick is then that g[1] is a cheap way to generate \$n\$ 1s. Since the sequence starting with [1] is just an endless stream of 1s. So we use g in two ways, and even though g might be slightly longer because it implements something a little more general, it saves bytes because it serves two purposes.

Bounty Ended with 150 reputation awarded by xnor
added 770 characters in body
Source Link
Wheat Wizard
  • 100k
  • 22
  • 282
  • 661

Haskell, 46 bytes

g _ 0=[]
g(h:t)n=h:g(t++[sum$h:t])(n-1)
g.g[1]

Attempt This Online!

This is based on xnor's answer but it employs 1 clever trick.

The \$n\$-bonacci sequence always start with \$n\$ 1s. A more general version of this might start with just any \$n\$ values. And this more general version is what we implement with g. g takes a list of \$n\$ integers and a value \$m\$ and gives us a list of the first \$m\$ terms of this generalized \$n\$-bonacci sequence.

The trick is then that g[1] is a cheap way to generate \$n\$ 1s. Since the sequence starting with [1] is just an endless stream of 1s. So we use g in two ways, and even though g might be slightly longer because it implements something a little more general it saves bytes because it serves two purposes.

Haskell, 46 bytes

g _ 0=[]
g(h:t)n=h:g(t++[sum$h:t])(n-1)
g.g[1]

Attempt This Online!

Haskell, 46 bytes

g _ 0=[]
g(h:t)n=h:g(t++[sum$h:t])(n-1)
g.g[1]

Attempt This Online!

This is based on xnor's answer but it employs 1 clever trick.

The \$n\$-bonacci sequence always start with \$n\$ 1s. A more general version of this might start with just any \$n\$ values. And this more general version is what we implement with g. g takes a list of \$n\$ integers and a value \$m\$ and gives us a list of the first \$m\$ terms of this generalized \$n\$-bonacci sequence.

The trick is then that g[1] is a cheap way to generate \$n\$ 1s. Since the sequence starting with [1] is just an endless stream of 1s. So we use g in two ways, and even though g might be slightly longer because it implements something a little more general it saves bytes because it serves two purposes.

Source Link
Wheat Wizard
  • 100k
  • 22
  • 282
  • 661

Haskell, 46 bytes

g _ 0=[]
g(h:t)n=h:g(t++[sum$h:t])(n-1)
g.g[1]

Attempt This Online!