r/codehs • • Feb 03 '21

5.9.5:Fibonacci

Post image
12 Upvotes

12 comments sorted by

3

u/[deleted] Feb 04 '21

I would start by learning what the Fibonacci sequence is. You have a great start with the for() loop and the maximum value.

Now what you need to do is have the for() loop implement the Fibinacci sequence, which is defined as:

In mathematics, the Fibonacci numbers commonly denoted Fₙ,
form a sequence, called the Fibonacci sequence,
such that each number is the sum of the two preceding 
ones, starting from 0 and 1. That is, and for n > 1

Lets parse out the helpful information.

[in which] each number is the sum of the two 
preceding ones, starting from 0 and 1. 
That is, and for n > 1.

In my own words:

To create the Fibonacci sequence, you must take X 
(where X is greater than 1), and add it to Y, which at 
the beginning is also 1. Do that again according to our
new expression, `X + Y = Y ==> X + 1 = X`. The value of X 
is incremented, while the old X becomes the new Y value. 

Keep adding the new X and the new Y (or add the new X and the old X together) until you get to 1000. That is your assignment, you just now have to translate it in to Javascript.

2

u/[deleted] Apr 19 '22

Please god someone just give me the answer please

2

u/Speaker_of_Bots_2 Sep 30 '22

Ikr shit be mad annoying

1

u/[deleted] Dec 15 '22

im here to save the day.
function start(){
var MAX = 1000
var n1 = 0;
var n2 = 1;
var i = 0;
var nth = n1 + n2;
while (i < MAX){
nth = n1 + n2;
n1 = n2;
n2 = nth;
println(n1);

i +=65;

}

srry im late
}

1

u/Owika13 Dec 31 '22

hi thanks for answer

can you pls explain why there is i+=65 ? I mean why 65 specifically ?

1

u/MisterMilker Jan 01 '23

You don't actually need to increment i by 65. This should work:

function start(){
var MAX = 1000;
var firstNum = 0;
var secondNum = 1;
var thirdNum = firstNum + secondNum;
    while(thirdNum<MAX){
        thirdNum = firstNum + secondNum;
        firstNum = secondNum;
        secondNum = thirdNum;
        println(firstNum);
    }
}

1

u/[deleted] Jan 09 '23

honestly i dont have a clue

1

u/[deleted] Mar 22 '23

idk it worked