Skip to content

Instantly share code, notes, and snippets.

@purushothblog
Last active December 31, 2015 12:09
Show Gist options
  • Save purushothblog/7984586 to your computer and use it in GitHub Desktop.
Save purushothblog/7984586 to your computer and use it in GitHub Desktop.
Check the given number is Fibonacci and find the next number in Fibonacci sequence.
var n = 4, fib;
var a = (5 * Math.pow(n, 2) + 4)
var b=(5 * Math.pow(n, 2) - 4 )
var result = Math.sqrt(a) % 1==0;
var res = Math.sqrt(b) % 1==0;
//fixed this line
if(result || res == true) // checks the given input is fibonacci series
{
fib=Math.round(n* 1.618); // finds the next fibonacci series of given input
console.log("The next Fibonacci number is " + fib);
}
else
{
console.log("The given number is not a fibonacci number");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment