In "real" terms, computing the Nth fibonacci number takes O(M(N)) = O(N log N 2^(O(log* N))) operations.
The Nth Fibonacci Number in O(log N)
11–20 of 46 posts
Re: The Nth Fibonacci Number in O(log N)
#12Re: The Nth Fibonacci Number in O(log N)
#13In Python: def fib(n): import math r5 = math.sqrt(5) x = pow((1 + r5) / 2, n) x -= pow((1 - r5) / 2, n) return x / r5 $ python fib.py 4 3.0 $ python fib.py 5 5.0 $ python fib.py 50 12586269025.0 $ python fib.py 5000 OverflowError: (34, 'Result too large') So we add arbitrary precision. Uglier, but supports any n: def fib(n): from math import sqrt from decimal import Decimal r5 = Decimal(sqrt(5)) x = pow((1 + r5) / 2,…
Edit: Looks like sillysaurus3 edited his comment.
Re: The Nth Fibonacci Number in O(log N)
#14In Python: def fib(n): import math r5 = math.sqrt(5) x = pow((1 + r5) / 2, n) x -= pow((1 - r5) / 2, n) return x / r5 $ python fib.py 4 3.0 $ python fib.py 5 5.0 $ python fib.py 50 12586269025.0 $ python fib.py 5000 OverflowError: (34, 'Result too large') So we add arbitrary precision. Uglier, but supports any n: def fib(n): from math import sqrt from decimal import Decimal r5 = Decimal(sqrt(5)) x = pow((1 + r5) / 2,…
Re: The Nth Fibonacci Number in O(log N)
#15 def __get_matrix_power(self, M, p):
if p == 1:
return M
if p % 2 == 1: # odd power
return self.__multiply_matrices(M, self.__get_matrix_power(M, p - 1))
else: # even power
K = self.__get_matrix_power(M, int(p/2))
return self.__multiply_matrices(K, K)Re: The Nth Fibonacci Number in O(log N)
#16In Python: def fib(n): import math r5 = math.sqrt(5) x = pow((1 + r5) / 2, n) x -= pow((1 - r5) / 2, n) return x / r5 $ python fib.py 4 3.0 $ python fib.py 5 5.0 $ python fib.py 50 12586269025.0 $ python fib.py 5000 OverflowError: (34, 'Result too large') So we add arbitrary precision. Uglier, but supports any n: def fib(n): from math import sqrt from decimal import Decimal r5 = Decimal(sqrt(5)) x = pow((1 + r5) / 2,…
This shows nothing but one's ability to code-up a math formula. The blog post goes into much detail about how to solve the problem using matrices and memoization. Edit: Looks like sillysaurus3 edited his comment.
Re: The Nth Fibonacci Number in O(log N)
#17I remember there is an equation that could get the Nth fibonacci number directly. (Maybe in the exercises of Concrete Mathematics by Knuth).
https://en.wikipedia.org/wiki/Fibonacci_number#Closed-form_e... If ever there was a way to impress an interviewer it is to bust out this baby when asked to make a fibonacci function.
Re: The Nth Fibonacci Number in O(log N)
#18It's easy to compute things quickly if you pretend that you can do arbitary-precision arithmetic in constant time. Want a quadrillion digits of Pi? Easy, just use the Brent–Salamin AGM algorithm and you'll get them in about 500 arithmetic operations. NP-complete problems? No problem at all, rephrase them as subset-sum problems and use the polynomial-integer-operations dynamic programming algorithm. I don't think you…
Re: The Nth Fibonacci Number in O(log N)
#19In Python: def fib(n): import math r5 = math.sqrt(5) x = pow((1 + r5) / 2, n) x -= pow((1 - r5) / 2, n) return x / r5 $ python fib.py 4 3.0 $ python fib.py 5 5.0 $ python fib.py 50 12586269025.0 $ python fib.py 5000 OverflowError: (34, 'Result too large') So we add arbitrary precision. Uglier, but supports any n: def fib(n): from math import sqrt from decimal import Decimal r5 = Decimal(sqrt(5)) x = pow((1 + r5) / 2,…
This shows nothing but one's ability to code-up a math formula. The blog post goes into much detail about how to solve the problem using matrices and memoization. Edit: Looks like sillysaurus3 edited his comment.
Re: The Nth Fibonacci Number in O(log N)
#20Earlier quoted context omitted.
This shows nothing but one's ability to code-up a math formula. The blog post goes into much detail about how to solve the problem using matrices and memoization. Edit: Looks like sillysaurus3 edited his comment.
I wasn't trying to one-up anyone. I thought it was interesting, so I figured others might find it interesting too. In particular, it was surprising to find that there's a closed-form solution for the Fibonacci sequence. Sorry.