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
22
votes
2 answers

How many ways are there to describe the Fibonacci sequence in Perl 6?

I've been looking at the various ways of constructing lazy lists in Perl 6 and I would like to collect all of the concise ways of describing the Fibonacci sequence. I will start this off with the three from masak's journal: my @fibs := (0, 1, -> $a,…
Eric Strom
  • 39,821
  • 2
  • 80
  • 152
21
votes
27 answers

Fibonacci sequence in Ruby (recursion)

I'm trying to implement the following function, but it keeps giving me the stack level too deep (SystemStackError) error. Any ideas what the problem might be ? def fibonacci( n ) [ n ] if ( 0..1 ).include? n ( fibonacci( n - 1 ) + fibonacci(…
Maputo
  • 963
  • 1
  • 10
  • 22
20
votes
8 answers

Why does (int)55 == 54 in C++?

So I'm learning C++. I've got my "C++ Programming Language" and "Effective C++" out and I'm running through Project Euler. Problem 1...dunzo. Problem 2...not so much. I'm working in VS2008 on a Win32 Console App. Whats the Sum of all even terms…
Restore the Data Dumps
  • 38,967
  • 12
  • 96
  • 122
20
votes
6 answers

fibonacci works in python but fails in Java

I have this code for calculating fibonacci number in python. It works and gives the expected result. but when I translated the same to Java, it fails. Any idea of what is going wrong here? In python: def fib3(n): a,b=0,1 while n>0: …
eagertoLearn
  • 9,772
  • 23
  • 80
  • 122
20
votes
13 answers

An iterative algorithm for Fibonacci numbers

I am interested in an iterative algorithm for Fibonacci numbers, so I found the formula on wiki...it looks straight forward so I tried it in Python...it doesn't have a problem compiling and formula looks right...not sure why its giving the wrong…
Ris
  • 1,892
  • 9
  • 26
  • 43
19
votes
11 answers

Generating Fibonacci series in F#

I'm just starting to learn F# using VS2010 and below is my first attempt at generating the Fibonacci series. What I'm trying to do is to build a list of all numbers less than 400. let fabList = let l = [1;2;] let mutable a = 1 let…
photo_tom
  • 7,292
  • 14
  • 68
  • 116
19
votes
6 answers

Writing a C# version of Haskell infinite Fibonacci series function

Note: The point of this question is more from a curiosity perspective. I want to know out of curiosity whether it is even possible to transliterate the Haskell implementation into a functional C# equivalent. So I've been learning myself Haskell for…
gideon
  • 19,329
  • 11
  • 72
  • 113
19
votes
2 answers

Why is a Fibonacci heap called a Fibonacci heap?

The Fibonacci heap data structure has the word "Fibonacci" in its name, but nothing in the data structure seems to use Fibonacci numbers. According to the Wikipedia article: The name of Fibonacci heap comes from Fibonacci numbers which are used in…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
18
votes
4 answers

Tail Recursion Fibonacci

How do I implement a recursive Fibonacci function with no loops running in O(n)?
Mat.S
  • 1,814
  • 7
  • 24
  • 36
18
votes
2 answers

Finding Fibonacci numbers using regex

I found the following code example on this blog post : final String FIBONACCI = "(?x) .? | ( \\2?+ (\\1|^.) )* .."; for (int n = 0; n < 10000; n++) { String s = new String(new char[n]); if (s.matches(FIBONACCI)) { …
Melih Altıntaş
  • 2,495
  • 1
  • 22
  • 35
17
votes
2 answers

Implement fibonacci in Clojure using map/reduce

Is it possible to implement the fibonacci series in Clojure efficiently using reduce? What would the "accumulator" contain? I imagine that it will have to be lazy. It's obvious how to do it using recursion or loop/recur.
Ralph
  • 31,584
  • 38
  • 145
  • 282
17
votes
1 answer

How to write a trait bound for adding two references of a generic type?

I have a Fibonacci struct that can be used as an iterator for anything that implements One, Zero, Add and Clone. This works great for all integer types. I want to use this struct for BigInteger types which are implemented with a Vec and are…
dten
  • 2,364
  • 21
  • 28
17
votes
2 answers

Whats the point of lazy-seq in clojure?

I am looking through some example Fibonacci sequence clojure code: (def fibs (lazy-cat [1 2] (map + fibs (rest fibs)))) I generally understand what is going on, but don't get the point of lazy-cat. I know that lazy-cat is a macro that is…
dbyrne
  • 59,111
  • 13
  • 86
  • 103
17
votes
5 answers

Count number of possible paths up ladder

I can't seem to come up with an algorithm to solve the following problem, I tried using a series of for-loops but it became way too complicated: A ladder has n steps, one can climb the ladder using any combination of steps of 1 or steps of 2.…
user1473018
16
votes
5 answers

Showing two different fibonacci functions are equivalent

I'm trying to learn exactly what it means to prove a program correct. I'm starting from scratch and getting hung up on the first steps/the introduction to the topic. In this paper on total functional programming, two definitions of the fibonacci…