I need to create a web server with rest service in node.js. The server have to find a given input is Fibonacci or not. If it is Fibonacci, find the next number in Fibonacci sequence.
So I just write a simple program for finding Fibonacci number, but it doesn't work.
Following are the Javascript functions I tried:
var n = 5,s, a, fib;
function isPerfectSquare(s)
{
var a = Math.sqrt(s);
return a * a == s;
}
function isFibonacci(n)
{
s = (5 * Math.pow(n, 2) + 4 || 5 * Math.pow(n, 2) - 4)
return s;
}
//How to fix this line. I don't know how to check perfect square conditions
if(isPerfectSquare(isFibonacci(n)) || isPerfectSquare(isFibonacci(n)))
{
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");
}
isPerfectSquaredoesnt work at all, try with something like =function(x){ return x >=0 && !(Math.sqrt(x) % 1); }. Also, node.js is great, but doesnt work well doing numeric or heavy cpu stuffs. A tipical example is0.1+0.3but you could google it to see more realistic problems.