Created
February 26, 2016 18:22
-
-
Save zzarcon/dae3db4b470cb5140fb5 to your computer and use it in GitHub Desktop.
Fibonacci loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fibonacci(num){ | |
var a = 1, b = 0, temp; | |
while (num >= 0){ | |
temp = a; | |
a = a + b; | |
b = temp; | |
num--; | |
} | |
return b; | |
} |
@shubich I was wondering the same thing. I guess if you change line 4 to "while (num > 0){" it will be correct.
This version doesn't require a temp variable
function fibonacci(num) {
var i = 1, j = 0
while (num >= 1) {
j = i + j
i = j - i
num--
console.log(j)
}
//return (j)
}
fibonacci(10)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why is your Fibonacci algorithm (0) equal to 1, but not 0?
In other sources I have seen "...while(num >= 1)...".
How it will be correct?