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

Memoizing fibonacci function in php

I've created a memoized function of the recursive version of fibonacci. I use this as an example for other kinds of functions that would use memoization. My implementation is bad since if I include it in a library, that means that the global…
catzilla
  • 1,901
  • 18
  • 31
6
votes
4 answers

Nth Fibonacci number for n as big as 10^19?

I am trying to make a program to find the nth Fibonacci number for 1 < n < 10^19. Here is my code using dynamic programming. memo = {} def fib(n): if n in memo: return memo[n] if n <= 2: f = 1 else: f = fib(n-1) +…
Mehdi Alali
  • 163
  • 1
  • 11
6
votes
4 answers

Representing Fibonacci numbers using a list comprehension in Haskell

I have written the following code to generate a list containing the Fibonacci numbers. fibonacci = [a + b | a <- 1:fibonacci, b <- 0:1:fibonacci] I would expect the output of the list to be [1,2,3,5,8,13..], however, the output is not the Fibonacci…
Sam
  • 3,070
  • 3
  • 20
  • 26
6
votes
3 answers

Python prevent overflow errors while handling large floating point numbers and integers

I am working on a python program to calculate numbers in the Fibonacci sequence. Here is my code: import math def F(n): return ((1+math.sqrt(5))**n-(1-math.sqrt(5))**n)/(2**n*math.sqrt(5)) def fib(n): for i in range(n): print…
Progo
  • 3,452
  • 5
  • 27
  • 44
6
votes
16 answers

Fibonacci series in C++

#include using namespace std; int main() { int num1 = 0; int num2 = 1; int num_temp; int num_next = 1; int n; cin >> n; for (int i = 0; i < n; i++){ cout << num_next << " "; num_next = num1 +…
user2943407
  • 409
  • 3
  • 5
  • 13
6
votes
5 answers

How does memoizing a recursive factorial function make it more efficient?

var lookup = {}; function memoized(n) { if(n <= 1) { return 1; } if(lookup[n]) { return lookup[n]; } lookup[n] = n * memoized(n - 1); return lookup[n]; } vs. function fact(n) { if(n <= 1) { return 1; } return n *…
ordinary
  • 5,943
  • 14
  • 43
  • 60
6
votes
6 answers

fibonacci series - recursive summation

Ok, I initially wrote a simple code to return the Fibonacci number from the series based on the user input.. n=5 will produce 3.. static int fibonacci(int n) { if (n == 1) return 0; else if (n == 2) return 1; …
Learner
  • 2,303
  • 9
  • 46
  • 81
6
votes
5 answers

Upper limits for fibonnacci

I was reading about the DP version of fibonnaci. In Sedgewick I saw: int[] T = new int[47]; for storage of the previous calculations. Elsewhere I saw that the max input for fibonacci should be less than 92. It is not clear to me how does these…
Cratylus
  • 52,998
  • 69
  • 209
  • 339
6
votes
1 answer

calculating Fibonacci in C#

I am trying to calculate the Fibonacci sequence in C# in a very simple way, however when it comes to the higher numbers it bugs out and stops working by giving out wrong answers. ulong num = 1; ulong lnum = 0; uint x = 1; private void…
6
votes
1 answer

Iterative deletion from list (Python 2)

I've just started programming, and I'm solving Project Euler problems with Python for practice. (This is problem #2, finding the sum of the even fibonacci numbers within 4 million.) My problem appears in the loop at the bottom, where I'm trying to…
zxz
  • 709
  • 1
  • 8
  • 19
6
votes
7 answers

In java, how would I find the nth Fibonacci number?

Determining the Fibonacci sequence is easy enough to figure out: int num = 0; int num2 = 1; int loop; int fibonacci; System.out.print(num2); for (loop = 1; loop <= 10; loop ++) { fibonacci = num + num2; num = num2; num2 = fibonacci; …
CydonPrax
  • 77
  • 1
  • 1
  • 5
6
votes
5 answers

Generating "own" Fibonacci sequence

I've got an unusual (I think) problem. For a given number F_n (I don't know the value of n), I have to find numbers F_0, F_1 such that F_{n}=F_{n-1}+F_{n-2}. The additional difficulty is that this sequence should be as long as possible (value n for…
xan
  • 1,053
  • 1
  • 10
  • 29
5
votes
7 answers

Project Euler #2 Infinity?

I am trying to solve Euler's Project #2 and I keep getting the answer as "Infinity" or "NaN" (Not a number) I tried changing the type of number to a int (originally Double), but that didn't fix anything just gave me the answer "-1833689714" public…
Spencer H
  • 450
  • 1
  • 6
  • 17
5
votes
2 answers

Recursive methods to create Streams in Scala

This is a follow-up to my previous question. As I understand the following method for calculating Fibonacci numbers is inefficient since the method fib is called for each Fibonacci number and each time it is called it creates a new stream. def…
Michael
  • 10,185
  • 12
  • 59
  • 110
5
votes
4 answers

Cleaning up Clojure function

Coming from imperative programming languages, I am trying to wrap my head around Clojure in hopes of using it for its multi-threading capability. One of the problems from 4Clojure is to write a function that generates a list of Fibonacci numbers of…
wespiserA
  • 3,131
  • 5
  • 28
  • 36