Message Boards Message Boards

0
|
2017 Views
|
1 Reply
|
0 Total Likes
View groups...
Share
Share this post:

Fibonacci Sequence in Loop Form

Posted 3 years ago

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?

POSTED BY: Abbey Hill

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}]
POSTED BY: Hans Dolhaine
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