Questions tagged [fibonacci]

The Fibonacci sequence is the sequence defined by F(0) = 0, F(1) = 1, F(n + 2) = F(n) + F(n + 1). The first few terms are 0, 1, 1, 2, 3, 5, 8.

The Fibonacci sequence is the sequence defined by

F(0) = 0
F(1) = 1
F(n + 2) = F(n) + F(n + 1).  

The first few terms are 0, 1, 1, 2, 3, 5, and 8.


The most efficient way to compute the first N values is to iterate over an array using the above formula, resulting in O(N) operations (O(N²) if digit or bit operations are counted). The recursive implementation should be generally avoided, since it is O(φN) where φ is the golden ratio and is equal to (1+sqrt(5))/2. However, by using a cache for already computed values, it can be as fast as the iterative implementation.


One efficient method for computing single Fibonacci numbers is

Fib(n) = Round(  Power( 0.5*(1+Sqrt(5)), n ) / Sqrt(5)  );

The square root and power have to be computed in sufficient precision, roughly, Fib(n) requires about 0.2*n decimal digits or about 0.7*n bits in the integer result.


Another method is based on the fact that the matrix

( Fib[n+1] Fib[ n ] )
( Fib[ n ] Fib[n-1] )

is the n-th power of the matrix

( 1 1 ) which is ( Fib[2] Fib[1] )
( 1 0 ) equal to ( Fib[1] Fib[0] )

This is the basis of a halving-and-squaring method that computes Fib[N] in O(log(N)) operations, completely in (big) integer arithmetic.

If one accounts for the complexity of big integer multiplication, the complexity raises to O( M(N) ) digit or bit operations, where M(d) is the complexity of the multiplication of numbers of bit length d. Typical methods have M(d)=O(d*log(d)) for FFT based multiplication, M(d)=O(dw) with w=log2(3) for Karatsuba and smaller w for the Toom-Cook methods.

2345 questions
395
votes
12 answers

Computational complexity of Fibonacci Sequence

I understand Big-O notation, but I don't know how to calculate it for many functions. In particular, I've been trying to figure out the computational complexity of the naive version of the Fibonacci sequence: int Fibonacci(int n) { if (n <= 1) …
Juliet
  • 80,494
  • 45
  • 196
  • 228
167
votes
37 answers

Java recursive Fibonacci sequence

Please explain this simple code: public int fibonacci(int n) { if(n == 0) return 0; else if(n == 1) return 1; else return fibonacci(n - 1) + fibonacci(n - 2); } I'm confused with the last line especially because if n…
Index Hacker
  • 2,004
  • 3
  • 17
  • 20
123
votes
4 answers

How is this fibonacci-function memoized?

By what mechanism is this fibonacci-function memoized? fib = (map fib' [0..] !!) where fib' 1 = 1 fib' 2 = 1 …
bjornars
  • 1,506
  • 2
  • 10
  • 13
81
votes
14 answers

How does the fibonacci recursive function "work"?

I'm new to Javascript and was reading up on it, when I came to a chapter that described function recursion. It used an example function to find the nth number of the Fibonacci sequence. The code is as follows: function fibonacci(n) { if (n <…
opes
  • 1,778
  • 1
  • 16
  • 22
79
votes
10 answers

Why are Fibonacci numbers significant in computer science?

Fibonacci numbers have become a popular introduction to recursion for Computer Science students and there's a strong argument that they persist within nature. For these reasons, many of us are familiar with them. They also exist within Computer…
Ian Bishop
  • 5,185
  • 3
  • 26
  • 37
78
votes
5 answers

How to write a generator class?

I see lot of examples of generator functions, but I want to know how to write generators for classes. Lets say, I wanted to write Fibonacci series as a class. class Fib: def __init__(self): self.a, self.b = 0, 1 def…
Pritam
  • 929
  • 1
  • 7
  • 16
77
votes
21 answers

Test if a number is fibonacci

I know how to make the list of the Fibonacci numbers, but i don't know how can i test if a given number belongs to the fibonacci list - one way that comes in mind is generate the list of fib. numbers up to that number and see if it belongs to the…
VaioIsBorn
  • 7,683
  • 9
  • 31
  • 28
77
votes
17 answers

nth fibonacci number in sublinear time

Is there any algorithm to compute the nth fibonacci number in sub linear time?
Biswajyoti Das
  • 7,881
  • 11
  • 34
  • 26
77
votes
24 answers

Finding out nth fibonacci number for very large 'n'

I was wondering about how can one find the nth term of fibonacci sequence for a very large value of n say, 1000000. Using the grade-school recurrence equation fib(n)=fib(n-1)+fib(n-2), it takes 2-3 min to find the 50th term! After googling, I came…
kunal18
  • 1,935
  • 5
  • 33
  • 58
73
votes
4 answers

Understanding a recursively defined list (fibs in terms of zipWith)

I'm learning Haskell, and came across the following code: fibs = 0 : 1 : zipWith (+) fibs (tail fibs) which I'm having a bit of trouble parsing, in terms of how it works. It's very neat, I understand that nothing more is needed, but I'd like to…
Frank
  • 4,341
  • 8
  • 41
  • 57
71
votes
10 answers

An inverse Fibonacci algorithm?

There are dozens of ways of computing F(n) for an arbitrary n, many of which have great runtime and memory usage. However, suppose I wanted to ask the opposite question: Given F(n) for n > 2, what is n? (The n > 2 restriction is in there since…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
67
votes
34 answers

Efficient calculation of Fibonacci series

I'm working on a Project Euler problem: the one about the sum of the even Fibonacci numbers. My code: def Fibonacci(n): if n == 0: return 0 elif n == 1: return 1 else: return Fibonacci(n-1) +…
user65165
  • 874
  • 1
  • 8
  • 11
65
votes
12 answers

Generating Fibonacci numbers in Haskell?

In Haskell, how can I generate Fibonacci numbers based on the property that the nth Fibonacci number is equal to the (n-2)th Fibonacci number plus the (n-1)th Fibonacci number? I've seen this: fibs :: [Integer] fibs = 1 : 1 : zipWith (+) fibs (tail…
Lucky
  • 4,787
  • 9
  • 40
  • 50
52
votes
10 answers

In the Fibonacci sequence, is fib(0) 0 or 1 ?

I'm doing a task in a subject were fib(0) is defined to = 1. But that can't be right? fib(0) is 0? Program with fib(0) = 1; spits out fib(4) = 5 Program with fib(0) = 0; spits out fib(3) = 3 What is the correct definition?
Algific
  • 1,470
  • 2
  • 18
  • 33
48
votes
52 answers

Generating Fibonacci Sequence

var x = 0; var y = 1; var z; fib[0] = 0; fib[1] = 1; for (i = 2; i <= 10; i++) { alert(x + y); fib[i] = x + y; x = y; z = y; } I'm trying to get to generate a simple Fibonacci Sequence but there no output. Can anybody let me know what's…
methuselah
  • 12,766
  • 47
  • 165
  • 315
1
2 3
99 100