0

Is there a way to find n fibonacci numbers starting from a given k? I know that the basic method would be to find all fibonacci numbers starting from 0, keep track of when a number in the series is greater than k, and then find n numbers from that point. But is there a simpler way?

What if I want to find only 3 fibonacci numbers after 5,000,000? Do I have to find all the numbers in the series starting from 0?

Also, if the only way to solve this would be to start from 0, then which approach would be better? The iterative or the recursive one?

Thanks.

drunkenfist
  • 2,958
  • 12
  • 39
  • 73
  • To answer your last question, iterative is going to be vastly more efficient than recursive on most - if not all - platforms. – 500 - Internal Server Error Sep 19 '14 at 00:30
  • Recursive approaches take longer, so iterative would be the way to go for speed. Take a look at the solution here, a similar method that might work for you: http://stackoverflow.com/a/24272606/499581 – l'L'l Sep 19 '14 at 00:31

3 Answers3

2

Using the golden ratio you can calculate Nth fibonacci.

phi = 1.61803...

Xn=(phi^n - (1-phi)^n) / sqrt(5)

Where n starts with 0.

http://en.wikipedia.org/wiki/Golden_ratio#Relationship_to_Fibonacci_sequence

This formula gives you the position of the number related to the next and previous Fibonacci number. That is, if the formula yields a natural number, it is the Nth Fibonacci number. If yields a number with decimals it belongs between the previous and next natural number. If the number is 2.7, it is between 2 and 3, so you are looking for fib(3), fib(4) and fib(5)

Or you can use Gessel formula.

A number is a Fibonacci if and only if

5*n^2+4 is a square number or 5*n^2-4 is a square number

So you could start counting from your ``N (in this example 5*10^6) until you hit the two first Fibonacci.

ssedano
  • 8,322
  • 9
  • 60
  • 98
  • Thanks, but how would I find 3 fibonacci numbers after 5,000,000? Should I plug in random values for X(n) such that X(n) > 5 mil and X(n-1) < 5 mil? Otherwise, this will be same as the iterative method. – drunkenfist Sep 19 '14 at 00:56
  • @drunkenfist Note that `(1-phi)^n` becomes vanishingly small as `n` becomes large, so the equation is approximately `Xn=phi^n / sqrt(5)` for large `n`. Which means that you can find the approximate value of `n` with `n=log(Xn*sqrt(5))/log(phi)`. – user3386109 Sep 19 '14 at 03:08
  • @user3386109 I'm not sure I quite understand what you are trying to say. But if you mean substituting the value of 5 mil in the above equation, then that would be wrong as 5 mil might not be a Fibonacci number. The problem does not say that the given 'k' is a Fibonacci number. It can be any random number. – drunkenfist Sep 19 '14 at 04:04
  • @drunkenfist If you substitute 5e6 in the equation, you get 33.7, which means that 5e6 is between Fibonacci numbers 33 and 34. Since you want the three numbers after 5000000, you want numbers 34, 35, and 36. – user3386109 Sep 19 '14 at 05:35
  • 1
    A couple notes for anyone using this formula with programming languages similar to C. 1) **performance:** the formula requires two calls to `pow()` (which is time consuming). It only takes 91 adds to compute every Fibonacci number that fits into 64-bits. On my machine, it takes 0.57 usec to compute all the 64-bit Fibonacci numbers using addition, and it takes 0.17 usec to compute one number with the formula. 2) **accuracy:** Since `double` precision floating point values only have about 53 bits of precision, the formula only works up to Fibonacci number 71. After that, rounding causes errors. – user3386109 Sep 19 '14 at 16:45
1

The fibonacci sequence grows exponentially, which means you don't have to do very many iterations before you're above 5 million. In fact, the 37th Fibonacci number is above 5 million.

So I wouldn't look further than naive iteration, here in Python:

def fib(a0, k):
    a, b = 0, 1
    while a < a0:
        a, b = b, a + b
    for _ in xrange(k):
        yield a
        a, b = b, a + b

print list(fib(5000000, 3))
Paul Hankin
  • 54,811
  • 11
  • 92
  • 118
0

You might want to check this out http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html

although I don't think it is worthy to use for big input of N, this method uses the binet's formula.

General Electric
  • 1,176
  • 3
  • 21
  • 44