Message Boards Message Boards

GROUPS:

Fibonacci Sequence in Loop Form

Posted 7 days ago
111 Views
|
1 Reply
|
0 Total Likes
|

I am trying to figure out to produce the first n digits of the Fibonacci sequence but in the form of a Do and While Loop.

I understand that we can define the Fibonacci sequence in the form of a recursive function where f[1] = 1 ; f[2] = 1; f[n_] := f[n] = f[n - 1] + f[n - 2]

How do I put this into a do and while loop to yield the first n terms of the sequence?

Why do you want a loop? Mathematica offers far more elegant solutions. Ok, you could do it like this

n = 15;
x = 1;
y = 1;
Print[x];
Print[y];
Do[
 z = x + y;
 Print[z, " ", Fibonacci[j + 2]];
 x = y;
 y = z,
 {j, 1, n}]
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract