math - The inverse Fibonacci algorithm? -


there dozens of ways of computing f(n) arbitrary n, many of have great runtime , memory usage.

however, suppose wanted ask opposite question:

given f(n) n > 2, n?

(the n > 2 restriction in there since f(1) = f(2) = 1 , there's no unambiguous inverse).

what efficient way of solving problem? it's easy in linear time enumerating fibonacci numbers , stopping when hit target number, there way of doing faster that?

edit: currently, best solution posted here runs in o(log n) time using o(log n) memory, assuming mathematical operations run in o(1) , machine word can hold number in o(1) space. i'm curious if it's possible drop memory requirements, since can compute fibonacci numbers using o(1) space.

since op has asked matrix solution not involving floating point computations, here is. can achieve o(logn) complexity way, assuming numeric operations have o(1) complexity.

let's take 2x2 matrix a having following structure

1 1 1 0 

now consider vector (8, 5), storing 2 consecutive fibonacci numbers. if multiply matrix, you'll (8*1 + 5*1, 8*1 + 5*0) = (13, 8) - next fibonacci number.
if generalize, a^n * (1, 0) = (f(n), f(n - 1)).

the actual algorithm takes 2 steps.

  1. calculate a^2, a^4, a^8, etc. until pass desired number.
  2. do binary search n, using calculated powers of a.

on side note, sequence of form f(n) = k1*f(n-1) + k2*f(n-2) + k3*f(n-3) + .. + kt*f(n-t) can presented this.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -