java - Fibbonacci sequence -
java - Fibbonacci sequence -
i'm having little troubling understanding code below. i've worked out vales each of variables each loop , understand how values each variable alter after each loop i'm confused how int = b; represents sum of 2 previous values. stuck on problem long time , solved problem through trial , error.
i don't understand how int = b; represents sum of 2 previous values. convinced since int c = + b; sums both variable , variable b variable wanted print in program. can explain how int represents sum of 2 previous values , why int c not.
public class fibonacci extends consoleprogram{ public void run(){ int = 0; int = 0; int b = 1; while ( <= 12) { println(a); i++; int c = + b; = b; b = c; } } }
i think of staircase:
0 0 + 1 = 1 1 + 1 = 2 1 + 2 = 3 2 + 3 = 5 3 + 5 = 8 5 + 8 = 13
an arbitrary step like:
a + b = c b + c = d
after 1 step, c
acts b
, b
acts a
. a
, d
? since solution iterative, a
becomes d
, repeat process 1 time again in loop:
a + b = c | b + c = |___________|
or in code:
int = 0; int b = 1; int c = 0; while (true) { c = + b; // `a + b = c` isn't valid, have flip around. = b; // `b` "becomes" `a` b = c; // `c` "becomes" `b` c = a; // don't need step because `c` temp variable }
java
Comments
Post a Comment