66
\$\begingroup\$

This isn't very widely known, but what we call the Fibonacci sequence, AKA

1, 1, 2, 3, 5, 8, 13, 21, 34...

is actually called the Duonacci sequence. This is because to get the next number, you sum the previous 2 numbers. There is also the Tribonacci sequence,

1, 1, 1, 3, 5, 9, 17, 31, 57, 105, 193, 355, 653, 1201...

because the next number is the sum of the previous 3 numbers. And the Quadronacci sequence

1, 1, 1, 1, 4, 7, 13, 25, 49, 94, 181, 349, 673...

And everybody's favorite, the Pentanacci sequence:

1, 1, 1, 1, 1, 5, 9, 17, 33, 65, 129...

And the Hexanacci sequence, the Septanacci sequence, the Octonacci sequence, and so on and so forth up to the N-Bonacci sequence.

The N-bonacci sequence will always start with N 1s in a row.

The Challenge

You must write a function or program that takes two numbers N and X, and prints out the first X N-Bonacci numbers. N will be a whole number larger than 0, and you can safely assume no N-Bonacci numbers will exceed the default number type in your language. The output can be in any human readable format, and you can take input in any reasonable manner. (Command line arguments, function arguments, STDIN, etc.)

As usual, this is Code-golf, so standard loopholes apply and the shortest answer in bytes wins!

Sample IO

#n,  x,     output
 3,  8  --> 1, 1, 1, 3, 5, 9, 17, 31
 7,  13 --> 1, 1, 1, 1, 1, 1, 1, 7, 13, 25, 49, 97, 193
 1,  20 --> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
 30, 4  --> 1, 1, 1, 1       //Since the first 30 are all 1's
 5,  11 --> 1, 1, 1, 1, 1, 5, 9, 17, 33, 65, 129
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Man, I had this idea a while ago and never got around to writing it up. \$\endgroup\$ Commented Jan 29, 2016 at 21:43
  • 2
    \$\begingroup\$ Wouldn't 3-bonacci be 1, 1, 2, 4, 7 as the third position would be 0 + 1 + 1? ... and so one with the others? \$\endgroup\$
    – Umbrella
    Commented Feb 8, 2019 at 17:42
  • 1
    \$\begingroup\$ @umbrella No, the tribonacci starts with 3 1s. See my edit to clarify this point. \$\endgroup\$
    – DJMcMayhem
    Commented Feb 8, 2019 at 17:51
  • \$\begingroup\$ Well then, that rules out using this approach. What a shame. \$\endgroup\$
    – Deadcode
    Commented Apr 11, 2021 at 7:39

44 Answers 44

1
2
1
\$\begingroup\$

Vyxal, 8 bytes

1ẋ{:∑pṫ,

Try it Online!

-2 bytes thanks to emanresu A

\$\endgroup\$
1
  • \$\begingroup\$ 8 \$\endgroup\$
    – emanresu A
    Commented Jul 1, 2022 at 2:08
1
\$\begingroup\$

JavaScript, 65 bytes

The eval & slice are annoying me!

x=>g=(n,...a)=>n?g(n-1,...a,a[x-1]?eval(a.slice(-x).join`+`):1):a

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Thunno 2, 13 bytes

{1}¹{K°ɱS}K¹ɱ

Attempt This Online!

Explanation

{1}¹{K°ɱS}K¹ɱ  # Implicit input
{1}            # Push 1 to the stack n times
   ¹{    }     # Repeat x times:
     K°ɱ       #  Take the top n items of the stack
        S      #  And sum this list
          K¹ɱ  # Take the top x items of the stack
               # Implicit output
\$\endgroup\$
0
\$\begingroup\$

PHP, 78 bytes

for(list(,$n,$x)=$argv;$i<$x;print${$i++}." ")$s+=$$i=$i<$n?1:$$d+$s-=${$d++};

Try it online!

-4 Bytes using PHP>=7.1 [,$n,$x] instead of list(,$n,$x)

\$\endgroup\$
0
\$\begingroup\$

Jq 1.5, 67 bytes

def C:if length>X then.[:X]else.+=[.[-N:]|add]|C end;[range(N)|1]|C

Assumes input provided by N and X e.g.

def N: 5;
def X: 11;

Expanded

def C:                        # . is current array
    if length>X               # stop when array is as long as X
    then .[:X]                # return first X elements
    else .+=[.[-N:]|add] | C  # recursively add sum of last N elements to array
    end
;
  [range(N)|1]                # initial state
| C

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Mathematica, 59 bytes

((f@#=1)&/@Range@#;f@n_:=Tr[f[n-#]&/@Range@#];f/@Range@#2)&

You'll probably want to Clear@f between function calls. Arguments are n,x, just like the test cases.

\$\endgroup\$
1
0
\$\begingroup\$

Tidy, 36 bytes

{x,n:n^recur(*tile(x,c(1)),sum@c,x)}

Try it online!

Explanation

{x,n:n^recur(*tile(x,c(1)),sum@c,x)}
{x,n:                              }   lambda taking parameters `x` and `n`
     n^                                take the first `n` terms of...
       recur(                     )        a recursive function
             *tile(x,c(1)),                whose seed is `x` `1`s
                           sum@c,          taking the sum of each window
                                 x         with a window size of `x`
\$\endgroup\$
0
\$\begingroup\$

Japt, 18 bytes

@ZsVn)x}gK=Vì1;K¯U

Try it online!

Explanation:

         K=Vì1        :Start with n 1s in an array K
@      }gK            :Extend K to at least x elements by setting each new element to:
      x               : The sum of
 ZsVn                 : The previous n elements
              ;       :Then
               K¯U    :Return the first n elements of K
\$\endgroup\$
0
\$\begingroup\$

MathGolf, 10 bytes

ª*kÆ_Σ▐├p;

Try it online!

Explanation

ª            push [1]
 *           pop a, b : push(a*b)
  k          read integer from input
   Æ         start block of length 5
    _        duplicate TOS
     Σ       sum(list), digit sum(int)
      ▐      append to end of list
       ├     pop from left of string/array
        p    print with newline
         ;   discard TOS
\$\endgroup\$
0
\$\begingroup\$

C# (.NET Core), 130 bytes

(n,x)=>{int j=0,k=0,l=0;var b=new int[x];for(;j<(x<n?x:n);)b[j++]=1;for(j=n;j<x;l=0){for(k=n;k>0;)l+=b[j-k--];b[j++]=l;}return b;}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Japt, 16 12 bytes

@ZÔ¯V x}hVÆ1

Try it

@ZÔ¯V x}hVÆ1     :Implicit input of integers U=X & V=N
         VÆ1     :Map the range [0,V) returning 1 for each element
        h        :Push the result of the following to the array and repeat until its length equals U
@                :  Pass the array through the following function as Z
 ZÔ              :    Reverse Z
   ¯V            :    Slice to the Vth element
      x          :    Reduce by addition
       }         :  End function
\$\endgroup\$
0
\$\begingroup\$

C (gcc), 94 bytes

a(c,C,i){for(int Q[C+c],*q=Q;C--;printf("%d ",*q++))for(*q=Q-q>(i=-c);Q-q<=i&i<0;)*q+=q[i++];}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

krrp, 72 bytes

^nx:\L[take]x,^nl:?nL#!fl@-n1#!r[append]lL[sum]lEl.x[map]^_:1.[range]0n.

Try it online!


Explanation

^nx:              ~ lambda expression in two parameters
 \L               ~  import list module
 [take]x          ~  the first x elements of
  ,^nl:           ~   extend the initial list of n ones
    ?n            ~    if n is non-zero
     L #!fl       ~     keep the first element
      @-n1        ~     recur
       #!r        ~     separate the first element
        [append]l ~      append an additional element,
         L[sum]lE ~      namely the sum of the previous elements
     l            ~    if n is zero, yield l
   . x            ~ extend x elements, yielding in a list of n+x elements
   [map]^_:1.     ~   map a constant function
   [range]0n.     ~   onto a list of n elements

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PostScript, 83 bytes

/f{/x exch def/n exch def[1 1 x{n le{1}{n copy n 1 sub{add}repeat}ifelse}for]==}def

e.g., 5 11 f prints [1 1 1 1 1 5 9 17 33 65 129].

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.