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

How to read jsben.ch benchmark result?

I've compared three fibonacci algorithms with jsben.ch and as expected first one is the fastest (it even got a little award icon): Yet, I can't find what the numbers next to code block result mean? The higher, the better but what's the unit?
iaforek
  • 2,860
  • 5
  • 40
  • 56
5
votes
3 answers

Ratio of leaves to total nodes in a Fibonacci call stack

If you were to look at a recursive implementation of calculating the nth Fibonacci number (root 100, children 99 and 98, grandchildren 98, 97, 97, and 96, etc. etc.), roughly what would be the ratio of the number of leaves to the total number of…
user666866
  • 329
  • 2
  • 8
5
votes
6 answers

Python variable assignment question

a,b = 0,1 while b < 50: print(b) a = b b = a+b outputs: 1 2 4 8 16 32 wheras: a,b = 0,1 while b < 50: print(b) a,b = b, a+b outputs (correct fibonacci sequence): 1 1 2 3 5 8 13 21 34 Aren't they the same? I mean a,b = b,…
eozzy
  • 66,048
  • 104
  • 272
  • 428
5
votes
1 answer

What are overlapping subproblems in Dynamic Programming (DP)?

There are two key attributes that a problem must have in order for dynamic programming to be applicable: optimal substructure and overlapping subproblems [1]. For this question, we going to focus on the latter property only. There are various…
5
votes
2 answers

how to format the float to 3 decimal in pinescript

I m using this alertcondition to ask the value from a plot, but the returned value has more than 3 decimal places. Can I format it to make the output only display precision until 3 decimals only? alertcondition(crossover(d2,d3), title="monitor",…
ZKTON
  • 61
  • 1
  • 2
5
votes
3 answers

Template Metaprogramming to calculate Fibonacci

Recently in a job interview I was asked to give the result of 100th element of a 3rd-class Fibonacci sequence(Fib(n)=Fib(n-1)+Fib(n-2)+Fib(n-3). I finished by Mathematical Induction and constructing a class to present numbers larger than long long.…
5
votes
2 answers

Fibonacci numbers python, what should i learn to be a better programmer (self taught )

n1 = 0 n2 = 1 fiblist = [] while True: newNum = n1 + n2 fiblist.append(newNum) n1 = n2 n2 = newNum if newNum >= 10000: print(flist) break beginner programmer: is there an easier way to write this or…
5
votes
1 answer

count the numbers such that a number must have its count of set bits as a fibonacci number?

Given a range [x,y] find the count of numbers such that a number must have its count of set bits as a fibonacci number? Eg: [15,17] 15 - 1111 - Count of bits is 4 - (4 is not a fibonacci number) 16 - 10000 - Count of bits is 1 - (1 is a fibonacci…
5
votes
3 answers

Fibonacci sequence works in python, but not in c?

I have the following python code: a, b = 1, 1 for i in range(0, 100): print a a, b = b, a + b It generates this: 1 1 2 3 5 8 etc I wrote the same in c: #include long long unsigned int a = 1, b = 1; void main(){ for(int i = 0;…
Programah
  • 179
  • 1
  • 10
5
votes
2 answers

Google foobar python: failure on two tests - lovely lucky lambs (counting of sequences)

I was invited to play Google's foobar challenge. I am currently on Level 2.2 with following question. Lovely Lucky LAMBs Being a henchman isn't all drudgery. Occasionally, when Commander Lambda is feeling generous, she'll hand out Lucky LAMBs…
gvavvari
  • 53
  • 1
  • 4
5
votes
5 answers

Is there something wrong with this python code, why does it run so slow compared to ruby?

I was interested in comparing ruby speed vs python so I took the simplest recursive calculation, namely print the fibonacci sequance. This is the python code #!/usr/bin/python2.7 def fib(n): if n == 0: return 0 …
rapadura
  • 5,242
  • 7
  • 39
  • 57
5
votes
2 answers

Is an iterative solution for Fibonacci series pseudo-polynomial?

So when we do an iterative solution to find the nth number in a Fibonacci sequence, we run a for loop (n-2) times. This would mean that the time complexity would be O(n). Is this correct or would it actually be pseudo-polynomial depending on the…
5
votes
1 answer

Fibonacci function doesn't calculate properly

I've defined this macro #define FIB(n) (( 4 << n*(3+n))/((4 << (2*n)) - (2 << n) - 1))%(2 << n) and when I try to get an answer, doesn't work properly, by example if I call FIB(7),it gives me 0, that clearly is wrong. I tested this function in…
Natali Torres
  • 303
  • 1
  • 4
  • 13
5
votes
1 answer

Poor Dynamic Programming Implementation or HashMap Slow?

I've got two Fibonacci methods I'm testing out. Both should be linear. Either I don't understand memoization or HashMap lookups are slower than I thought. I understand that the recursive function shouldn't be sped up a lot by the addition of DP,…
5
votes
1 answer

Can someone explain this lazy Fibonacci solution?

This is the code: fibs = 0 : 1 : zipWith (+) fibs (drop 1 fibs) When evaluated, fibs is an infinite list of Fibonacci numbers. What I don't understand is how the list is concatenated. zipWith returns a list, so zipping fibs would yield this: 0 :…
dopatraman
  • 13,416
  • 29
  • 90
  • 154