Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if a number is fibonacci

I know how to make the list of the Fibonacci numbers, but i don't know how can i test if a given number belongs to the fibonacci list - one way that comes in mind is generate the list of fib. numbers up to that number and see if it belongs to the array, but there's got to be another, simpler and faster method.

Any ideas ?

like image 624
VaioIsBorn Avatar asked Mar 12 '10 12:03

VaioIsBorn


People also ask

How do you check if a number is a Fibonacci number python?

num=int(input("Enter the number you want to check\n")) temp=1 k=0 a=0 summ=0 while summ<=num: summ=temp+k temp=summ k=temp if summ==num: a=a+1 print("Yes. {} is a fibonnaci number".

How do you check whether a number is Fibonacci or not in Java?

Step 1 : Declare one variable called inputNumber and store the user entered number in it. Step 2 : initialize firstTerm = 0, secondTerm = 1 and thirdTerm = 0 where firstTerm and secondTerm are the first two numbers of the series.


1 Answers

A very nice test is that N is a Fibonacci number if and only if 5 N^2 + 4 or 5N^2 – 4 is a square number. For ideas on how to efficiently test that a number is square refer to the SO discussion.

Hope this helps

like image 117
Il-Bhima Avatar answered Oct 15 '22 19:10

Il-Bhima