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

ConcurrentHashMap and Fibonacci Numbers - Inconsistent result

I wrote a program for calculating fibonacci numbers recursively, with a ConcurrentHashMap and computeIfAbsent() method: Program works absolutely fine when i used small values like 8,9,10 but stuck in endless loop when value increased from 10 to…
T-Bag
  • 10,916
  • 3
  • 54
  • 118
16
votes
13 answers

How can I create the fibonacci series using a list comprehension?

I am new to python, and I was wondering if I could generate the fibonacci series using python's list comprehension feature. I don't know how list comprehensions are implemented. I tried the following (the intention was to generate the first five…
Thenavats
  • 175
  • 1
  • 1
  • 5
15
votes
2 answers

Multi-threaded parallelism performance problem with Fibonacci sequence in Julia (1.3)

I'm trying the multithread function of Julia 1.3 with the following Hardware: Model Name: MacBook Pro Processor Name: Intel Core i7 Processor Speed: 2.8 GHz Number of Processors: 1 Total Number of Cores: 4 L2 Cache (per Core): 256 KB L3…
ecjb
  • 5,169
  • 12
  • 43
  • 79
15
votes
2 answers

Understanding recursion with the Fibonacci Series

I am trying to better understand recursion and how return statements work. As such, I'm looking at a piece of code meant to identify the fibonacci number associated with a given term -- in this case, 4. I'm have difficulty understanding the else…
efw
  • 449
  • 3
  • 16
15
votes
1 answer

Why does Java regex engine throw StringIndexOutOfBoundsException on a + repetition?

I've written a regex pattern to find Fibonacci numbers (it doesn't matter why, I just did). It works wonderfully as expected (see on ideone.com): String FIBONACCI = "(?x) .{0,2} | (?: (?=(\\2?)) (?=(\\2\\3|^.)) (?=(\\1)) \\2)++ . "; …
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
14
votes
15 answers

Recursive Fibonacci memoization

I need some help with a program I'm writing for my Programming II class at universtiy. The question asks that one calculates the Fibonacci sequence using recursion. One must store the calculated Fibonacci numbers in an array to stop unnecessary…
Eogcloud
  • 1,335
  • 4
  • 19
  • 44
14
votes
3 answers

Time Complexity of Memoization Fibonacci?

I have the memoization fibonacci code and I am having trouble figuring out what the time complexity is of it: function fibMemo(index, cache) { cache = cache || []; if (cache[index]) return cache[index]; else { if (index < 3) return 1; …
georgej
  • 3,041
  • 6
  • 24
  • 46
14
votes
10 answers

Algorithm for k-Fibonacci

We all know fibonacci series, when k = 2. I.e.: 1,1,2,3,5,8,13 But this is the 2-fibonacci. Like this, I can count the third-fibonacci: 1,1,2,4,7,13,24 And the 4-fibonacci: 1,1,2,4,8,15,29 ...and so goes on What I'm asking is an algorithm to…
Gabriel L. Oliveira
  • 3,922
  • 6
  • 31
  • 40
14
votes
22 answers

How to generate Fibonacci faster

I am a CSE student and preparing myself for programming contest.Now I am working on Fibonacci series. I have a input file of size about some Kilo bytes containing positive integers. Input formate looks like 3 5 6 7 8 0 A zero means the end of…
Mehedi
  • 141
  • 1
  • 1
  • 6
14
votes
4 answers

Calculate the number of representations of a number as a sum of fibonacci numbers

My group struggled to find a good algorithm but all we could come up with was an exponential one. Is there a way to make it faster? Here's the full question: Define a function function F(n:Integer):Integer; which will calculate the number of…
Dunno
  • 3,632
  • 3
  • 28
  • 43
13
votes
2 answers

Fibonacci Seq. strange output forms (Haskell)

I was watching the results of my Fibobacci sequence implementation in Haskell when I realised some "strange" forms in the optput of the numbers. First of all, this is the Haskell code I've come up with: fib :: Integer -> [Integer] fib 0 = [0] fib…
12
votes
4 answers

binary heap vs binomial heap vs fibonacci heap, regarding performance for a priority queue

Could someone please explain me how should I decide whether to use one or another heap implementation, among the ones mentioned in the title? I would like an answer to guide me on choosing the implementation regarding the performance of the…
Throoze
  • 3,988
  • 8
  • 45
  • 67
12
votes
3 answers

In Excel, how to round to nearest fibonacci number

In Excel, I would like to round to the nearest fibonacci number. I tried something like (sorry with a french Excel): RECHERCHEH(C7;FIBO;1;VRAI) -- HLOOKUP(C7, FIBO, 1, TRUE) where FIBO is a named range (0; 0,5; 1;2;3;5;8;etc.) my problem is that…
Cédric
  • 123
  • 1
  • 4
12
votes
14 answers

The lisp-way to solve Fibonnaci

I wanted to try and learn Lisp, but I very quickly gave up. I figured I'd try again. I'm looking at Problem 2 on Project Euler - finding the sum of all even Fibonacci numbers under 4 Million. I wrote the following code which works, but is all kinds…
Tom Ritter
  • 99,986
  • 30
  • 138
  • 174
12
votes
9 answers

Calculating fibonacci

I was sent this nice non-recursive function for computing a fibonacci sequence. So I coded up a bit of c# and was able to verify all numbers up to 1474 were correct. The problem comes in when attempting to calculate it for 1475 and above. My c#…
NotMe
  • 87,343
  • 27
  • 171
  • 245