title: Can a Fibonacci function be written to execute in O(1) time?
body: <p>So, we see a lot of fibonacci questions.  I, personally, hate them.  A lot.  More than a lot.  I thought it'd be neat if maybe we could make it impossible for anyone to ever use it as an interview question again.  Let's see how close to O(1) we can get fibonacci.</p>
  
  <p>Here's my kick off, pretty much crib'd from Wikipedia, with of course plenty of headroom.  Importantly, this solution will detonate for any particularly large fib, and it contains a relatively naive use of the power function, which places it at O(log(n)) at worst, if your libraries aren't good. I suspect we can get rid of the power function, or at least specialize it.   Anyone up for helping? Is there a true O(1) solution, other than the finite* solution of using a look-up table?</p>
  
  <p><a href="http://ideone.com/FDt3P" rel="noreferrer">http://ideone.com/FDt3P</a> </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;math.h&gt;
  using namespace std; // would never normally do this.
  
  int main()
  {
  int target = 10;
  cin &gt;&gt; target;
  // should be close enough for anything that won't make us explode anyway.
  float mangle = 2.23607610; 
  
  float manglemore = mangle;
  ++manglemore; manglemore = manglemore / 2;
  manglemore = pow(manglemore, target);
  manglemore = manglemore/mangle;
  manglemore += .5;
  cout &lt;&lt; floor(manglemore);
  
  }
  </code></pre>
  
  <p>*I know, I know, it's enough for any of the zero practical uses fibonacci has.</p>