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
12
votes
13 answers

Why is .NET faster than C++ in this case?

Make sure you run outside of the IDE. That is key. -edit- I LOVE SLaks comment. "The amount of misinformation in these answers is staggering." :D Calm down guys. Pretty much all of you were wrong. I DID make optimizations. It turns out whatever…
user34537
12
votes
5 answers

Fibonacci Rabbits Dying After Arbitrary # of Months

So, I've seen a few solutions for this problem or similar problems, but I really want to know why mine isn't working. It is much easier to read than a lot of solutions I've found, so I'd love to make it work! Starting with 1 pair of rabbits, which…
spacecadet
  • 251
  • 1
  • 3
  • 9
12
votes
1 answer

Looping through a formula that describes a spiral to generate XY coordinates

I'm trying to generate a spiral galaxy in the form of xy (2D) coordinates -- but math is not my strong suit. I've gleaned the following from an excellent source on spirals: The radius r(t) and the angle t are proportional for the simpliest…
Nick
  • 2,872
  • 3
  • 34
  • 43
11
votes
6 answers

Fibonacci Sum of Large Numbers(Only Last Digit to be Printed)

I have been trying to come out with a solution regarding the problem of finding the last digit of the sum of large n Fibonacci series. I have been able to pass several test cases with large n. But I'm stuck at the following case where n =…
codeyourstack
  • 341
  • 1
  • 2
  • 11
11
votes
8 answers

Create faster Fibonacci function for n > 100 in MATLAB / octave

I have a function that tells me the nth number in a Fibonacci sequence. The problem is it becomes very slow when trying to find larger numbers in the Fibonacci sequence does anyone know how I can fix this? function f = rtfib(n) if (n==1) f=…
Rick T
  • 3,349
  • 10
  • 54
  • 119
11
votes
9 answers

How does the recursion here work?

Code 1: public static int fibonacci (int n){ if (n == 0 || n == 1) { return 1; } else { return fibonacci (n-1) + fibonacci (n-2); } } How can you use fibonacci if you haven't gotten done explaining what it…
David
  • 14,569
  • 34
  • 78
  • 107
11
votes
14 answers

Generating a list of EVEN numbers in Python

Basically I need help in generating even numbers from a list that I have created in Python: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, ...] I have tried a couple different…
user1486654
  • 111
  • 1
  • 1
  • 4
10
votes
8 answers

Counting the bits set in the Fibonacci number system?

We know that, each non negative decimal number can be represented uniquely by sum of Fibonacci numbers(here we are concerned about minimal representation i.e- no consecutive Fibonacci numbers are taken in the representation of a number and also each…
CSStudent
  • 309
  • 2
  • 12
10
votes
4 answers

Fibonacci numbers without using zipWith

I have been trying to implement a list of Fibonacci number sequence from 0 to n without using the lazy zipwith method. What I have so far is code that returns a list from n to 1. Is there any way I can change this code so it returns the list from…
Jimbhoy
  • 195
  • 8
10
votes
4 answers

Tail call optimization for fibonacci function in java

I was studying about Tail call recursion and came across some documentation that mentioned. Sun Java doesn't implement tail call optimization. I wrote following code to calculate fibonacci number in 3 different ways: 1. Iterative 2. Head…
TTP
  • 101
  • 1
  • 4
10
votes
3 answers

How many additional function calls does fib(n) require if "LINE 3" is removed?

I just got this question on an interview and had no idea how to calculate the answer. How many additional function calls does fib(n) require if "LINE 3" is removed? The answer should be in terms on n. int fib(int n) { if(n == 0) return 0; if(n…
BROCK
  • 273
  • 1
  • 7
10
votes
4 answers

OverflowError 'Numerical result out of range' when generating fibonacci numbers

Possible Duplicate: Handling very large numbers in Python I have a python function to generate fibonacci numbers: def fib(n): …
fergusdawson
  • 1,645
  • 3
  • 17
  • 20
9
votes
1 answer

Partitions of values in a fibonacci call graph (call graph is a binary tree)

I have an ongoing project investigating the Fibonacci sequence, this is just a personal project, I have created a binary tree class which makes a binary tree of the Fibonacci call graph, so for f(3) I generate the tree: I want to create a method of…
Alex2134
  • 557
  • 7
  • 22
9
votes
25 answers

Finding the sum of even valued terms in Fibonacci sequence

#!/usr/bin/python2 """ Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibonacci…
yetanotherstacker
  • 285
  • 2
  • 4
  • 12
9
votes
3 answers

Why is one of these dynamic programming implementations of the Fibonacci sequence faster than the other?

I've been researching and benchmarking various Fibonacci algorithms recently for my own amusement and more or less by accident came up with an alternate implementation of the classic O(n) time and O(1) space dynamic programming…