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
9
votes
16 answers

Fibonacci series in JavaScript

function fib(n) { const result = [0, 1]; for (var i = 2; i <= n; i++) { const a = (i - 1); const b = (i - 2); result.push(a + b); } return result[n]; } console.log(fib(8)); The output of the code above is 13. I…
sumon
  • 147
  • 1
  • 1
  • 5
9
votes
6 answers

Determining the individual letters of Fibonacci strings?

The Fibonacci strings are defined as follows: The first Fibonacci string is "a" The second Fibonacci string is "bc" The (n + 2)nd Fibonacci string is the concatenation of the two previous Fibonacci strings. For example, the first few Fibonacci…
user577395
  • 125
  • 1
  • 6
9
votes
4 answers

Why does computational time decrease when removing unnecessary items from a list in Python

The past days I've been trying get a better understanding of computational complexity and how to improve Python code. For this I have tried out different functions for calculating Fibonacci numbers, comparing how long the script runs if I make small…
Jérôme Bau
  • 707
  • 5
  • 16
9
votes
5 answers

Finding the sum of Fibonacci Numbers

What would be the most efficient way to calculate the sum of Fibonacci numbers from F(n) to F(m) where F(n) and F(m) are nth and mth Fibonacci numbers respectively and 0 =< n <= m <109 (with F(0)=0, F(1)=1). For example, if n=0, m=3, we need to find…
pranay
  • 2,339
  • 9
  • 35
  • 58
9
votes
5 answers

Prolog; try to make fibonacci more effective?

This logic programming is really making a lap dance on my imperative programming skills. This is homework, so please just don't drop me the answer. This is what I have: fibo(N,1) :- N < 2, !. fibo(N,R) :- N1 is N-1, N2 is N-2, …
Algific
  • 1,470
  • 2
  • 18
  • 33
9
votes
9 answers

fibonacci numbers, why does this recurring function work?

I'm going through a programming book and one of the examples is about fibonacci numbers, and how a recurring function finds the fibonacci number for the nth one along. The code looks like this: Int fib (int n) { If (n<3) Return (1); Else Return…
Joseph
  • 3,899
  • 10
  • 33
  • 52
9
votes
2 answers

Space leak with recursive list zipWith

My space leak happens in one of my personal project. But I don't want someone to solve it in my project. I want to understand it. I reproduced my space leak by making up this algorithm: u is a sequence defined by: u(0) = 1 u(1) = 2 u(2) = 1 u(4) =…
Antoine Catton
  • 380
  • 3
  • 16
9
votes
3 answers

Parallelize Fibonacci sequence generator

I'm learning about parallelization and in one exercise I'm given a couple of algorithms that I should improve in performance. One of them is a Fibonacci sequence generator: array[0] = 0; array[1] = 1; for (q = 2; q < MAX; q++) { array[q] =…
Jawap
  • 2,463
  • 3
  • 28
  • 46
9
votes
11 answers

Python: Fibonacci Sequence

I'm just trying to improve my programming skill by making some basic functions. I want to fill a list with fibonacci values, but I think my code gives the sum of all the numbers put together and prints that instead.. numberlist = [] i = 0 for i in…
Da Bx
  • 129
  • 1
  • 1
  • 4
8
votes
3 answers

Value or constructor is not defined

I'm learning f# and I've got a pretty trivial problem that doesn't seem to make sense. I'm working on Project Euler problem 2 and I've got this: let fib (x : BigInteger) (y : BigInteger) (max : BigInteger) = let added = x + y if added > max…
Steven Evers
  • 16,649
  • 19
  • 79
  • 126
8
votes
2 answers

How to fix my Fibonacci stream in Scala

I defined a function to return Fibonacci stream as follows: def fib:Stream[Int] = { Stream.cons(1, Stream.cons(2, (fib zip fib.tail) map {case (x, y) => println("%s + %s".format(x, y)); x + y})) } The functions work ok but it looks…
Michael
  • 10,185
  • 12
  • 59
  • 110
8
votes
2 answers

Hughes' Fibonacci stream

I am trying to understand the "Streams as arrows" section in John Hughes' famous "Generalising Arrows to Monads". To be more precise, I am interested in writing down the Fibonacci stream. I tweaked Hughes' definition a bit: data StreamProcessor a b…
Zhiltsoff Igor
  • 1,812
  • 8
  • 24
8
votes
5 answers

Fibonacci Tree-Recursion in Structure and Interpretation of Computer Programs

In the classic text by Abelson/Sussman, Structure and Interpretation of Computer Programs, in Section 1.2.2 on tree recursion and the Fibonacci sequence, they show this image: The tree-recursive process generated in computing for the 5th Fibonacci…
sicpfan
  • 81
  • 3
8
votes
3 answers

Recursive Fibonacci in Assembly

I'm attempting to implement a recursive Fibonacci program in Assembly. However, my program crashes, with an unhandled exception, and I can't seem to pick out the problem. I don't doubt that it involves my improper use of the stack, but I can't seem…
muttley91
  • 12,278
  • 33
  • 106
  • 160
8
votes
1 answer

Numpy matrix exponentiation gives negative value

I wanted to use NumPy in a Fibonacci question because of its efficiency in matrix multiplication. You know that there is a method for finding Fibonacci numbers with the matrix [[1, 1], [1, 0]]. I wrote some very simple code but after increasing n,…
Rockybilly
  • 2,938
  • 1
  • 13
  • 38