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

Recursive function using MIPS assembly

I'm having some trouble on an assignment and would appreciate some help. I'm not asking for the answer, I prefer to put two and two together to figure it out myself, but I know so little about MIPS its hard for me to know where to start. Here is…
TheDubiousDubber
  • 359
  • 1
  • 3
  • 11
8
votes
2 answers

Haskell Fibonacci Explanation

I am quite new to Haskell and I'm trying to wrap my head around how the lazy expression of Fibonacci sequences work. I know this has been asked before, but none of the answers have addressed an issue I'm having with visualising the result. The code…
8
votes
4 answers

Infinite fibonacci sequence

I'm trying to imitate Haskell's famous infinite fibonacci list in F# using sequences. Why doesn't the following sequence evaluate as expected? How is it being evaluated? let rec fibs = lazy (Seq.append (Seq.ofList [0;1]) …
is7s
  • 3,500
  • 1
  • 20
  • 41
8
votes
2 answers

Is fibonacci search faster than binary search?

I read some materials that claims Fibonacci search is faster than binary search on average,and the main cause is "it involves only addition and subtraction,not division by 2". I have some questions: 1.Is Fibonacci search faster than binary search…
Mobility
  • 3,117
  • 18
  • 31
8
votes
4 answers

Calculate the Fibonacci number (recursive approach) in compile time (constexpr) in C++11

I wrote the program Fibonacci number calculation in compile time (constexpr) problem using the template metaprogramming techniques supported in C++11. The purpose of this is to calculate the difference in the run-time between the template…
Mantosh Kumar
  • 5,659
  • 3
  • 24
  • 48
8
votes
3 answers

Julia for image processing and speech recognition

I recently stumbled upon Julia Language and I was surprised to see their claims. It claims to be many folds faster than languages like Python, which I'm currently using for machine learning algorithms on speech recogniton. Their claim for example…
Anoop
  • 5,540
  • 7
  • 35
  • 52
8
votes
5 answers

Algorithm function for fibonacci series

I'm not looking necessarily for an answer, but I am looking for what this question is asking of. Found this question studying for an interview but not sure what they're asking? Write function that runs through the Fibonacci sequence and returns …
KingKongFrog
  • 13,946
  • 21
  • 75
  • 124
8
votes
2 answers

How does this memoized fibonacci function work?

In the current exercise assignment of the Functional Programming course I'm doing, we've got to make a memoized version of a given function. To explain memoization, the following example is given: fiblist = [ fibm x | x <- [0..]] fibm 0 = 0 fibm 1…
user42179
  • 431
  • 4
  • 11
8
votes
2 answers

How does this naive recursive fibonacci implementation not stackoverflow?

As a joke a few months ago, a coworker of mine sought out to "speed up the heat death of the universe" by calculating fibonacci numbers using this exponential algorithm: int Fib(int n) { if (n <= 1) return 1; else return…
Earlz
  • 62,085
  • 98
  • 303
  • 499
8
votes
2 answers

Memoization in OCaml?

It is possible to improve "raw" Fibonacci recursive procedure Fib[n_] := If[n < 2, n, Fib[n - 1] + Fib[n - 2]] with Fib[n_] := Fib[n] = If[n < 2, n, Fib[n - 1] + Fib[n - 2]] in Wolfram Mathematica. First version will suffer from exponential…
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
8
votes
9 answers

Returning Nth Fibonacci number the sequence?

I have a question on my homework for class and I need to know how to return nth number of Fibonacci sequence using iteration (no recursion allowed). I need some tips on how to do this so I can better understand what I am doing wrong. I output to the…
user1766351
  • 97
  • 1
  • 1
  • 4
7
votes
12 answers

Print a string of fibonacci recursively in C#

Can that be done with no while loops? static void Main(string[] args) { Console.WriteLine("Please enter a number"); int number = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(" #" + Fibonacci(number)); } public static int…
Marin
  • 12,531
  • 17
  • 56
  • 80
7
votes
1 answer

Recursive Fibonacci using Fork (in C)

I'm attempting to write a function that recursively computes the resulting fibonacci number from a given int n using forks in C. Here is the function specification: If print is true, print it. Otherwise, provide it to the parent process. The…
CODe
  • 2,253
  • 6
  • 36
  • 65
7
votes
5 answers

How do I get the last number from the range() function?

Is there a way to get the last number from the range() function? I need to get the last number in a Fibonacci sequence for first 20 terms or should I use a list instead of range()?
user94725
7
votes
8 answers

What is the fastest way to write Fibonacci function in Scala?

I've looked over a few implementations of Fibonacci function in Scala starting from a very simple one, to the more complicated ones. I'm not entirely sure which one is the fastest. I'm leaning towards the impression that the ones that uses…
Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156