Skip to content

Instantly share code, notes, and snippets.

@jsphkhan
Created June 27, 2018 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsphkhan/5d2d179c43777b8987319ed4b3c82b8e to your computer and use it in GitHub Desktop.
Save jsphkhan/5d2d179c43777b8987319ed4b3c82b8e to your computer and use it in GitHub Desktop.
Find the sequence of a given Fibonacci number.
/*
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