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

tail recursion and fibonacci

I was looking at this site: http://rosettacode.org/wiki/Fibonacci_sequence#JavaScript and saw this program: function fib(n) { return function(n,a,b) { return n>0 ? arguments.callee(n-1,b,a+b) : a; }(n,0,1); } How does this work, what are…
qwertymk
  • 34,200
  • 28
  • 121
  • 184
7
votes
1 answer

Partial application versus pattern matching: why do these Haskell functions behave differently?

I'm trying to understand something about Haskell functions. First, here is a Fibonacci function defined in the typical "slow" way (i.e. recursive with no memoization, and no infinite-list tricks) slowfib :: Int -> Integer slowfib 0 = 0 slowfib 1 =…
7
votes
1 answer

What is the real answer to SICP 3.57

SICP Exercise 3.57: How many additions are performed when we compute the nth Fibonacci number using the definition of fibs based on the add-streams procedure? Show that the number of additions would be exponentially greater if we had implemented…
nate
  • 71
  • 4
7
votes
1 answer

How to draw fibonacci sequence using turtle module

This is my first question ever, and I am a complete and utter beginner, so please don't eat me :) What I am trying to to is to draw a fibonacci sequence using the Python turtle module. My code is as follows: import turtle zuf =…
Ejdzbikej
  • 71
  • 2
  • 6
7
votes
2 answers

Calculating Fibonacci Numbers in C++ code

My question is: I have a matrix. I need to calculate the corresponding Fibonacci number to each entry in that matrix, and return those values into another matrix. I keep getting a C2109 "Subscript requires array or pointer type", and I know where…
Sam
  • 71
  • 4
7
votes
1 answer

How is this piece of Recursive lambda call in Java working

I recently came across this piece of code in Java. It involves Function and printing fibonacci numbers and it works. public class AppLambdaSubstitution { public static Function Y(Function,…
kau
  • 372
  • 1
  • 3
  • 14
7
votes
1 answer

Draw Fibonacci Arcs

I am attempting to create an application that draws Fibonacci Arcs similar to these. However, I'd like full circles instead of arcs, and I'd like to draw more than the three Fibonacci lines shown in the picture. I've created an application using…
Gabriel
  • 624
  • 1
  • 7
  • 20
7
votes
3 answers

Performance of Fibonacci

f[0] = 0; f[1] = 1; f[x_] := f[x-1] + f[x-2] This function is running slow in Mathematica and I need to increase the speed. I have to use functional programming and recursion. I'm not sure why this is running so slow, and even the slightest idea…
Jon
  • 71
  • 2
7
votes
1 answer

Greedy algorithm for finding a negafibonacci representation of a number?

According to Zeckendorf's theorem, every positive integer can be written in a unique way as the sum of non-consecutive distinct Fibonacci numbers. Such a decomposition can be found easily with a greedy algorithm consisting essentially in subtracting…
7
votes
4 answers

Fast Fibonacci computation

I saw a comment on Google+ a few weeks ago in which someone demonstrated a straight-forward computation of Fibonacci numbers which was not based on recursion and didn't use memoization. He effectively just remembered the last 2 numbers and kept…
Dmitry Rubanovich
  • 2,471
  • 19
  • 27
7
votes
2 answers

Haskell infinite recursion

The following function computes the Fibonacci sequence: fib = 0 : 1 : (zipWith (+) fib (tail fib)) If we run it, we will get an infinite list, but how does the recursion work? Why does it get to print numbers on the screen if it the function keeps…
7
votes
3 answers

How can I recursively generate an Array of the Fibonacci sequence?

I've seen several posts on generating a given fibonacci sequence, such as this one. However, I can't figure out how to generate the sequence (return an array) of fibonnaci numbers for a given n using recursion. What I have clearly doesn't work, but…
TheRealFakeNews
  • 7,512
  • 16
  • 73
  • 114
7
votes
4 answers

JavaScript: calculate the sum of all even numbers in Fibonacci sequence values < 10000

I have to complete this exercise, and I am not getting the results I need. The specifications are: Calculate the sum of all even numbers in a Fibonacci sequence for values under 10,000. The first few numbers summed would be: 2, 8, 34, 144, 610. I…
erasmo carlos
  • 664
  • 5
  • 16
  • 37
7
votes
1 answer

Fibonacci Series in Assembly x86

Finally after a long session of countless errors , hope this is the final one. No Compile or runtime errors, Just a logical error. EDIT: (Fixed Pseudocode) My Pseudocode: first = 1; second = 1; third = 0; for i from 1 to n{ …
Idkwhoami
  • 145
  • 1
  • 10
7
votes
6 answers

C print first million Fibonacci numbers

I am trying to write C code which will print the first 1million Fibonacci numbers. UPDATE: The actual problem is I want to get the last 10 digits of F(1,000,000) I understand how the sequence works and how to write the code to achieve that however…
Chris Edwards
  • 1,518
  • 2
  • 13
  • 23