# 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 ? – Sektor Aug 12 '15 at 17:57
• @Sektor For example, generate the first 100 values of the Fibonacci sequence starting with { 0, 1 } – Tyler Durden Aug 12 '15 at 18:01
• Ponder on the result of Accumulate[Fibonacci[Range[-1, 18]]] === Fibonacci[Range]. You probably would be more interested in LinearRecurrence[]. – J. M. is away Aug 12 '15 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

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

 fibSequences

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

• Nice! Wish I'd seen that myself. – Mr.Wizard Aug 13 '15 at 1:42
• @Mr.Wizard, Thanks:) – xyz Aug 13 '15 at 2:50
• Probably a good time for Nothing: Nest[Accumulate[{1, 0} ~Join~ #] &, {If[OddQ[n], 1, Nothing]}, Quotient[n, 2]] – J. M. is away Aug 13 '15 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 '15 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}