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
47
votes
23 answers

Fibonacci numbers, with an one-liner in Python 3?

I know there is nothing wrong with writing with proper function structure, but I would like to know how can I find nth fibonacci number with most Pythonic way with a one-line. I wrote that code, but It didn't seem to me best way: >>> fib = lambda…
utdemir
  • 26,532
  • 10
  • 62
  • 81
35
votes
20 answers

Python Fibonacci Generator

I need to make a program that asks for the amount of Fibonacci numbers printed and then prints them like 0, 1, 1, 2... but I can't get it to work. My code looks the following: a = int(raw_input('Give amount: ')) def fib(): a, b = 0, 1 while…
John
  • 351
  • 1
  • 3
  • 3
35
votes
13 answers

Recursive Fibonacci

I'm having a hard time understanding why #include using namespace std; int fib(int x) { if (x == 1) { return 1; } else { return fib(x-1)+fib(x-2); } } int main() { cout << fib(5) << endl; } results in a…
Ian Burris
  • 6,325
  • 21
  • 59
  • 80
34
votes
8 answers

Making Fibonacci faster

I was required to write a simple implementation of Fibonacci's algorithm and then to make it faster. Here is my initial implementation public class Fibonacci { public static long getFibonacciOf(long n) { if (n== 0) { return…
alainlompo
  • 4,414
  • 4
  • 32
  • 41
32
votes
7 answers

Can a Fibonacci function be written to execute in O(1) time?

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…
Jake Kurzer
  • 1,532
  • 4
  • 16
  • 25
31
votes
9 answers

a recursive Fibonacci function in Clojure

I'm a newcomer to clojure who wanted to see what all the fuss is about. Figuring the best way to get a feel for it is to write some simple code, I thought I'd start with a Fibonacci function. My first effort was: (defn fib [x, n] (if (< (count x)…
richc
  • 333
  • 1
  • 3
  • 6
29
votes
9 answers

Why is the complexity of computing the Fibonacci series 2^n and not n^2?

I am trying to find complexity of Fibonacci series using a recursion tree and concluded height of tree = O(n) worst case, cost of each level = cn, hence complexity = n*n=n^2 How come it is O(2^n)?
Suri
  • 3,287
  • 9
  • 45
  • 75
28
votes
3 answers

IEnumerable Skip on unlimited sequence

I have a simple implementation of Fibonacci sequence using BigInteger: internal class FibonacciEnumerator : IEnumerator { private BigInteger _previous = 1; private BigInteger _current = 0; public void…
rbm
  • 3,243
  • 2
  • 17
  • 28
27
votes
36 answers

Fibonacci Code Golf

Generate the Fibonacci sequence in the fewest amount of characters possible. Any language is OK, except for one that you define with one operator, f, which prints the Fibonacci numbers. Starting point: 25 14 characters in Haskell: …
Claudiu
  • 224,032
  • 165
  • 485
  • 680
26
votes
23 answers

What is a good example of recursion other than generating a Fibonacci sequence?

Possible Duplicates: Real-world examples of recursion Examples of Recursive functions I see that most programming language tutorial teach recursion by using a simple example which is how to generate fibonacci sequence, my question is, is there…
uray
  • 11,254
  • 13
  • 54
  • 74
24
votes
17 answers

Fibonacci One-Liner

I'm trying to solve questions from Project Euler in Ruby one-liners, and I'm curious if there's a more elegant solution for question two: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and…
clem
  • 3,524
  • 3
  • 25
  • 41
24
votes
9 answers

Fast Fibonacci recursion

I'm trying to recall an algorithm on Fibonacci recursion. The following: public int fibonacci(int n) { if(n == 0) return 0; else if(n == 1) return 1; else return fibonacci(n - 1) + fibonacci(n - 2); } is not what I'm looking for…
ducin
  • 25,621
  • 41
  • 157
  • 256
23
votes
7 answers

Generate a sequence of Fibonacci number in Scala

def fibSeq(n: Int): List[Int] = { var ret = scala.collection.mutable.ListBuffer[Int](1, 2) while (ret(ret.length - 1) < n) { val temp = ret(ret.length - 1) + ret(ret.length - 2) if (temp >= n) { return ret.toList …
nobody
  • 2,709
  • 6
  • 35
  • 37
23
votes
14 answers

Fibonacci closure in go

I am following the go tour on their official website and I have been asked to write a Fibonacci generator. Here it is: package main import "fmt" // fibonacci is a function that returns // a function that returns an int. func fibonacci() func()…
Bula
  • 2,398
  • 5
  • 28
  • 54
22
votes
7 answers

Why is my recursive function so slow in R?

The following takes about 30 seconds to run whereas I would expect it to be nearly instant. Is there a problem with my code? x <- fibonacci(35); fibonacci <- function(seq) { if (seq == 1) return(1); if (seq == 2) return(2); return…
deltanovember
  • 42,611
  • 64
  • 162
  • 244