It is interesting to read this Quora thread
What is the most efficient algorithm to check if a number is a Fibonacci Number? The top two answers are both very good and efficient.
Background, what is Fibonacci number? wikipedia
Anders gave a nice matrix exponentiation algorithm in Haskell while John posted his Java solution using square numbers. There was an argument which one was faster, so I made this comparison using python.
First of all, John needed a faster algorithm to determine if a number is a perfect square number. He used a loop from 2 to sqrt(N), and it was surely not an efficient way.
A better way to do it is the newton’s method of integer square number (wikipedia), in short, it equals using newton’s method to solve x*x-n = 0. In python, it can be easily solved by this (original code from this link):
def isqrt(x): if x < 0: raise ValueError('square root not defined for negative numbers') n = int(x) if n == 0: return 0 a, b = divmod(n.bit_length(), 2) x = 2**(a+b) while True: y = (x + n//x)//2 if y >= x: return x x = y
And then, John’s method is basically testing if 5*N*N+4 or 5*N*N-4 is a perfect square number. If the answer is yes for either one, this number is a Fibonacci number.
Actually the square root algorithm can have more optimizations using the 64 bit magic number 0x5fe6eb50c7b537a9 (the DOOM trick), please check wikipedia for more interesting details. To be platform independent, here I just used the original newton’s method.
Secondly, Anders code was in Haskell, so I rewrote them into Python for a fair comparison.
def fibPlus((a,b),(c,d)): bd = b*d return (bd-(b-a)*(d-c), a*c+bd) def unFib((a,b),n): if n<a: return (0,0,1) else: (k,c,d) = unFib(fibPlus((a,b),(a,b)),n) (e,f) = fibPlus((a,b),(c,d)) if n<e: return (2*k, c, d) else: return (2*k+1,e,f) def isfib(n): (k,a,b) = unFib((1,1),n) return n==a
The full source code can be found on my github https://github.com/phunterlau/checkFibonacci
To test these two algorithm, I downloaded the first 500 Fibonacci numbers from http://planetmath.org/listoffibonaccinumbers and ran 100 times for each algorithm on this list of number. The result is interesting: python optimization makes difference. Unit is in second.
If run in python 2.7, John’s method won for 10% time:
python is_fib.py Anders method: 1.52931690216 John method: 1.36000704765
If run in pypy 1.9, Anders method is highly optimized for 2x speed:
pypy is_fib.py Anders method: 0.799499988556 John method: 2.0126721859
To conclude, both of these two algorithms are very good.
One another question to follow this:
Given a number N, if N is not a Fibonacci number, print out the largest Fibonacci smaller than N.
Hint: John’s method.