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
5
votes
15 answers

Java Program Fibonacci Sequence

I am writing a "simple" program to determine the Nth number in the Fibonacci sequence. Ex: the 7th number in the sequence is: 13. I have finished writing the program, it works, but beginning at the 40th number it begins to delay, and takes longer,…
Javadork
  • 51
  • 1
  • 1
  • 3
5
votes
3 answers

Haskell- Find element in a list and return its position

So i need to make a function described as invFib :: Integer -> Maybe Integer which takes an Integer and looks for it in the fibonacci sequence (as described by the function below) fibs :: [Integer] fibs = 0:1:(zipWith (+) fibs (tail fibs)) and…
lopezrican304
  • 175
  • 1
  • 2
  • 7
5
votes
2 answers

How to use BigInteger with an array?

I'm working on altering my Fibonacci sequencer so that the numbers after reaching ~100th term don't wrap around and become negative. How do I use BigInteger in this code that I wrote: package me.kevinossia.mystuff; import java.util.Scanner; public…
Kevin Ossia
  • 179
  • 1
  • 3
  • 9
5
votes
2 answers

Regarding the Fibonacci Sequence example in Python's function tutorial

This is what they have: def fib(n): a, b = 0, 1 while a < n: print a, a, b = b, a+b This is what I have: def fib(n): a = 0 b = 1 while a < n: print a a = b b = b+a The first returns the…
5
votes
3 answers

Is there a better way (performance) calculate fibonacci than this one?

I made this code.. And I need to get the best of it.. I really need the best performance of calculating fibonacci numbers.. please help be.. I've read some code of this type of calculation and I think I got the best of them.. Avaliate this for…
thiagoh
  • 7,098
  • 8
  • 51
  • 77
5
votes
2 answers

Recursion on Fibonacci Sequence

I need some help in understanding the processing that happens here, so let´s say I call fib(5) I want the fibonacci 5, which is 8. But my brain in trying to understand the algorithm says it´s not. This is how i (wrongly) think: return fib(4) +…
Tom Lilletveit
  • 1,872
  • 3
  • 31
  • 57
5
votes
3 answers

Calculating nth fibonacci number using the formulae in python

I am calculating the n-th fibonacci number using (a) a linear approach, and (b) this expression Python code: 'Different implementations for computing the n-th fibonacci number' def lfib(n): 'Find the n-th fibonacci number iteratively' a, b…
Sumit
  • 1,934
  • 4
  • 17
  • 23
4
votes
3 answers

Calculating Fibonacci Number accurately in C++?

I am really confused. I am trying to calculate Fibonacci numbers, but as they get larger and large the numbers start to become wrong. and I do not know why. How do you calculate accurate Fibonacci numbers using Binet's Formula, it is my…
aJynks
  • 677
  • 2
  • 14
  • 27
4
votes
2 answers

How can I recursively insert the Fibonacci sequence into a binary tree

Hope someone can help, I'm not a programmer, but have been interested in exploring Fibonacci sequence and it's recursive tree... I've created a Binary Tree class, along with an associated TreeNode class, and want to generate a binary tree of the…
4
votes
1 answer

Graph:: Deletion Contraction Complexity?

I am applying the classic deletion contraction algorithm to a Graph G of "n" vertices and "m" edges. Z(G) = Z(G-e) + Z(G/e) In Wikipedia, http://en.wikipedia.org/wiki/Chromatic_polynomial#Deletion.E2.80.93contraction They say that complexity is:…
labotsirc
  • 722
  • 7
  • 21
4
votes
3 answers

How can I add certain values that were produced in a 'while' loop using Python

I was solving a Project Euler problem that goes as follows: By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms." So I used this script to print the Fibonacci sequence…
Grant
  • 891
  • 1
  • 9
  • 15
4
votes
9 answers

Why does this Fibonacci number function return infinity for the 4000th value?

Consider: function fibo() { var first, second, add; for(var i=0; i<4000; i++) { if(i === 0) { first = 1; second = 2; } add = first + second; first = second; second = add; } …
vetri02
  • 3,199
  • 8
  • 32
  • 43
4
votes
2 answers

OCaml creating a list of Fibonacci numbers

I have a function that generates Fibonacci numbers: let rec fib n = match n with | (0 | 1) -> 1 | x when x > 0 -> (fib (x-2) + fib (x-1)) | _ -> raise (Invalid_argument "Negative value supplied to fib");; but what I really want is…
user1044459
  • 57
  • 1
  • 5
4
votes
2 answers

Fibonacci implementation in assembly giving unexpected results

I'm trying to write an assembly code version of Fibonacci which gives the nth Fibonacci number and returns it. For some reason it is having trouble storing the return value of the Fibonacci numbers and adding them. I want it to print the nth…
NONE
  • 233
  • 6
  • 14
4
votes
2 answers

How do I show the fibonacci recursive tree on python

This is my code currently: from loguru import logger def fibonacci(n, s="% s"): """ Using recursive method """ # logger.debug(f"Finding {n}th Fibonacci number") logger.debug(s % ("fib(%d)" % (n))) a = 0 b = 1 if n…
user6308605
  • 693
  • 8
  • 26