1

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");
}
5
  • What is a perfect square by your definition? Should the length of the side be a non-decimal number? Commented Dec 11, 2013 at 10:05
  • No, It should be a decimal number. Commented Dec 11, 2013 at 10:10
  • isPerfectSquare doesnt 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 is 0.1+0.3 but you could google it to see more realistic problems. Commented Dec 11, 2013 at 10:12
  • We can use binet's formula to find given number is fibonacci. Binet's formula is if and only if (5*n^2 +4) or (5 *n^2 -4) is perfect square or both of them. Commented Dec 11, 2013 at 10:12
  • @bduran Thanks for your suggestion. Yeah, Its interesting but I couldn't find the better stuff for beginners like me. Can you re-write this program? Commented Dec 11, 2013 at 12:37

1 Answer 1

2

Finally, I found the solution for my own Fibonacci sequence question. Thank you for your suggestions guys.

The following snippet is working for me.

function isFibonacci(n) {
  var fib,
    a = (5 * Math.pow(n, 2) + 4),
    b = (5 * Math.pow(n, 2) - 4)

  var result = Math.sqrt(a) % 1 == 0,
    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 ${n} is not a fibonacci number`);
  }
}

$('#fib').on("keyup change", function() {
  isFibonacci(+this.value)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="fib" type="number" min=0 placeholder="Enter numebr.." />

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.