It'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…
The Nth Fibonacci Number in O(log N)
41–46 of 46 posts
Re: The Nth Fibonacci Number in O(log N)
#42Earlier quoted context omitted.
Yeah, but "kids these days" only want to know Java or Angular.js and bawl at the first sight of math.
Psh, who wants to learn Java?
Re: The Nth Fibonacci Number in O(log N)
#43http://coding.derkeiler.com/Archive/Java/comp.lang.java.prog...
The thread discusses the exponential, linear and logarithmic algorithms.
Re: The Nth Fibonacci Number in O(log N)
#44Earlier quoted context omitted.
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.
The closed form solution is not that difficult to derive at all if you put the problem as a reoccurrence equation with boundary conditions.
Re: The Nth Fibonacci Number in O(log N)
#45Earlier quoted context omitted.
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.
It would be impressive if you could derive or prove it, not if you'd just memorized it.
Re: The Nth Fibonacci Number in O(log N)
#46Earlier quoted context omitted.
Personally, I think the matrix powers method is more impressive. You're using the matrix as a state engine to get to whatever state you intend to reach. That has more applications than just the closed form, which a lot of people know about, and you most likely just happened to memorize. Don't get me wrong, it'd still be impressive "Woah, he memorized this!" but seeing someone do the matrix trick would be even more im…
The closed form is just the diagonalized version of the matrix power, though.
Now, if you derived the formula on the board from the matrix, that'd be incredibly impressive. The idea here is how the person's brain thinks. When I see them write an equation on the board, I think "Oh, they memorized it" when I see them step through the process of creating a matrix and doing matrix multiplication, I think "Wow, they /really/ know their stuff". If they just threw up the matrix and couldn't explain it, I wouldn't be as impressed. Same as if they wrote the equation and couldn't explain it.
At the end of the day, if they could explain either approach, I'd be impressed, I think the matrix form just lends itself to more likely be in a way that the person can explain it