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
1 answer

Stack overflows and recursive sequence expressions F#

I have a sequence expression like so: let fibSeq = let rec fibSeq' a b = seq { yield a yield! fibSeq' b (a + b) } fibSeq' 1 1 Now even for large numbers, this will not generate a stack overflow. I'm wondering why, it…
Overly Excessive
  • 2,095
  • 16
  • 31
5
votes
2 answers

Possibilities of Climbing n stairs with a constraint on the 20th floor

Im solving a recursion problem in which the function int stairs(int n) returns the number of possibilities in climbing the stairs up to n, with the conditions of either taking 1 step or 2 steps. The following code solves this: int stairs(int n) { …
A Elo
  • 87
  • 1
  • 4
5
votes
2 answers

Why is one memoization strategy slower than the other?

So this page about memoization got me curious. I ran my own benchmarks. 1) Mutable default dictionary: %%timeit def fibo(n, dic={}) : if n not in dic : if n in (0,1) : dic[n] = 1 else : dic[n] =…
usual me
  • 8,338
  • 10
  • 52
  • 95
5
votes
5 answers

Ruby Fibonacci algorithm

The following is a method I wrote to calculate a value in the Fibonacci sequence: def fib(n) if n == 0 return 0 end if n == 1 return 1 end if n >= 2 return fib(n-1) + (fib(n-2)) end end It works…
user3769323
  • 307
  • 1
  • 3
  • 15
5
votes
3 answers

Finding next fibonacci number

I need to find a (the next) fibonacci number given a integer N. So let's say I have n = 13 and I need to output the next fibonacci number which is 21 but how do I do this? How can I find the previous number that summed up to form it? I mean I could…
Mike Fonder
  • 61
  • 2
  • 6
5
votes
11 answers

Project Euler #2 in "Python"

I am an absolute beginner here. I was giving the questions on Project Euler a try in Python. Can you please point out where does my code go wrong? Q) Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting…
Ashtrix
  • 147
  • 1
  • 1
  • 11
5
votes
4 answers

Program to generate fibonacci series in GNU Prolog is giving an instantiation error

This is my code:- fib(0,0). fib(1,1). fib(F,N) :- N>1, N1 is N-1, N2 is N-2, F is F1+F2, fib(F1,N1), fib(F2,N2), write(F," ,"). On consulting in GNU Prolog, I am getting: | ?-…
user3341381
  • 53
  • 1
  • 1
  • 4
5
votes
2 answers

Fibonacci numbers with initial two values as parameters

I have been trying to make a infinite fibonacci list producing function that can take first 2 values as parameters. Without specifying the first two values it is possible like this fib = 1 : 1 : zipWith (+) fib (tail fib) Suppose I wanted to start…
Aseem Bansal
  • 6,722
  • 13
  • 46
  • 84
5
votes
3 answers

Prevent backtracking after first solution to Fibonacci pair

The term fib(N,F) is true when F is the Nth Fibonacci number. The following Prolog code is generally working for me: :-use_module(library(clpfd)). fib(0,0). fib(1,1). fib(N,F) :- N #> 1, N #=< F + 1, F #>= N - 1, F #> 0, N1 #= N - 1, …
Martin Klinke
  • 7,294
  • 5
  • 42
  • 64
5
votes
2 answers

How do I get fibonacci numbers using sliding Stream in scala?

There is a good example in Stream document that gets fibonacci numbers. val fibs:Stream[Int] = 0 #:: 1 #:: fibs.zip(fibs.tail).map { n => n._1 + n._2 } I would like to implement that by using sliding, so I tried followings. val test = 0 #:: 1 #::…
nineclue
  • 51
  • 4
5
votes
5 answers

Fibonacci Sequence - Find the number of digits - JavaScript

So, I have successfully written the Fibonacci sequence to create an array with the sequence of numbers, but I need to know the length (how many digits) the 500th number has. I've tried the below code, but its finding the length of the scientific…
Nicholas Hazel
  • 3,758
  • 1
  • 22
  • 34
5
votes
1 answer

Multi-recursive functions

I’d like to be pointed toward a reference that could better explain recursion when a function employs multiple recursive calls. I think I get how Python handles memory when a function employs a single instance of recursion. I can use print…
jmike
  • 55
  • 2
  • 6
5
votes
1 answer

Fibonacci Trace

I just want to be upfront that this is a homework assignment. I have attempted the problem in so many different ways that I am just out of ideas for why I am not getting the desired output. Problem Write a program that will trace how Fibonacci…
5
votes
3 answers

JavaScript Fibonacci breakdown

I hope it is ok that I am posting this question here even though I have also posted it on other sites. If I have failed to follow proper protocols, I apologize and please let me know right away so I may remove the post and learn my lessons. I've…
laserface
  • 449
  • 1
  • 4
  • 12
5
votes
4 answers

NumberFormatException: Infinite or NaN

I have a method that takes n and returns nth Fibonacci number. Inside the method implementation I use BigDecimal to get the nth Fibonacci number then I use method toBigInteger() to get the number as a BigInteger object and that's surely because I am…
kzidane
  • 753
  • 3
  • 11
  • 29