Created
June 27, 2018 17:44
-
-
Save jsphkhan/5d2d179c43777b8987319ed4b3c82b8e to your computer and use it in GitHub Desktop.
Find the sequence of a given Fibonacci number.
This file contains 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
/* | |
Say you are given a number from Fibonacci series - 13, find its sequence number | |
Input - 13, Output - 7 | |
Input - 21, Output - 8 | |
*/ | |
function findFibSeq(num) { | |
var a = 1, b = 1, sum = 0, n = 2; | |
//continue to run until sum and provided number is not equal | |
while(sum !== num) { | |
if(sum > num) { | |
return -1; //given number is not a number from fibonacci series eg. 15 | |
} | |
sum = a + b; | |
a = b; | |
b = sum; | |
n++; | |
} | |
return n; //return the index once found | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment