# Generate the Fibonacci sequence with Accumulate

Is there a way to generate the Fibonacci sequence with the Accumulate function?

• Generate a specific number, generate up to a specific number ? Aug 12, 2015 at 17:57
• @Sektor For example, generate the first 100 values of the Fibonacci sequence starting with { 0, 1 } Aug 12, 2015 at 18:01
• Ponder on the result of Accumulate[Fibonacci[Range[-1, 18]]] === Fibonacci[Range[20]]. You probably would be more interested in LinearRecurrence[]. Aug 12, 2015 at 18:03

fibSequences[n_?EvenQ] := Nest[Accumulate[Join[{1, 0}, #]] &, {}, n/2]
fibSequences[n_?OddQ] := Most@Nest[Accumulate[Join[{1, 0}, #]] &, {}, (n + 1)/2]


fibSequences[10]

 {1, 1, 2, 3, 5, 8, 13, 21, 34, 55}

 fibSequences[9]

 {1, 1, 2, 3, 5, 8, 13, 21, 34}

• Nice! Wish I'd seen that myself. Aug 13, 2015 at 1:42
• @Mr.Wizard, Thanks:)
– xyz
Aug 13, 2015 at 2:50
• Probably a good time for Nothing: Nest[Accumulate[{1, 0} ~Join~ #] &, {If[OddQ[n], 1, Nothing]}, Quotient[n, 2]] Aug 13, 2015 at 17:12
• @J. M., It is the first time for me to know the built-in Nothing.Thanks :) I searched the DOC, then I known that it was introduced in V10.2
– xyz
Aug 14, 2015 at 1:12

Well... I did use Accumulate!

First /@ NestList[{Last @ #, Last @* Accumulate @ #} &, {0, 1}, 10]
{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55}