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
3 answers

Assembly Language (x86): How to create a loop to calculate Fibonacci sequence

I am programming assembly language (x86) in MASM using Visual Studio 2013 Ultimate. I am trying to use an array to calculate a Fibonacci sequence for n elements using an array. In other words, I am trying to go to an array element, obtain the two…
jshapy8
  • 1,983
  • 7
  • 30
  • 60
7
votes
3 answers

Finding the nth fib number, in O(logn)

I am trying to solve this: SPOJ problem. And after some research I found out that it comes down to a simple calculation of the nth fib number, however n can get really large so an O(n) solution won't do any good. Googling around, I found that you…
user2947605
7
votes
4 answers

Fibonacci Sequence (JS) - Sum of Even Numbers

I started Project Euler. I am on problem 2 and came up with this code to come up with the sum of even fibonacci numbers up to 4 million. The code seems to do pretty much what I want it to. I do see the correct sum listed when the code is ran. The…
jayneedle32
  • 79
  • 1
  • 5
7
votes
14 answers

Determining whether a number is a Fibonacci number

I need to to write a Java code that checks whether the user inputed number is in the Fibonacci sequence. I have no issue writing the Fibonacci sequence to output, but (probably because its late at night) I'm struggling to think of the sequence of…
Emily
  • 71
  • 1
  • 1
  • 2
7
votes
1 answer

Uniformly arranging points on a sphere using Fibonacci Lattices

I'm trying to arrange points more-or-less uniformly along the surface of a unit sphere. I'm told that while this problem is difficult, Fibonacci Lattices give a very good solution. I've been trying for a couple of days to follow the very simple…
Ozzah
  • 10,631
  • 16
  • 77
  • 116
7
votes
4 answers

Haskell Fibonacci seems slow

I'm learning Haskell, and I wrote a simple Fibonacci function: fib :: Int -> Int fib 1 = 1 fib 0 = 0 fib n = (fib (n-1)) + (fib (n-2)) It seems to compile ok, and loading this script into the GHCI REPL I could mess around with a few numbers. I…
user485498
7
votes
8 answers

Non Brute Force Solution to Project Euler 25

Project Euler problem 25: The Fibonacci sequence is defined by the recurrence relation: Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. Hence the first 12 terms will be F1 = 1, F2 = 1, F3 = 2, F4 = 3, F5 = 5, F6 = 8, F7 = 13, F8 = 21, F9 = 34, F10 =…
slinky773
  • 143
  • 1
  • 8
7
votes
2 answers

Python func_dict used to memoize; other useful tricks?

A Python function object has an attribute dictionary called func_dict which is visible from outside the function and is mutable, but which is not modified when the function is called. (I learned this from answers to a question I asked yesterday…
behindthefall
  • 1,485
  • 1
  • 17
  • 18
7
votes
9 answers

How do I print a fibonacci sequence to the nth number in Python?

I have a homework assignment that I'm stumped on. I'm trying to write a program that outputs the fibonacci sequence up the nth number. Here's what I have so far: def fib(): n = int(input("Please Enter a number: ")) if n == 1: …
Al GoRhythm
  • 133
  • 2
  • 4
  • 12
7
votes
3 answers

Fibonacci in Scheme

I am trying to understand recursion in Scheme and I have a hard time doing the dry run for it, for example a simple Fibonacci number problem. Could someone break down the steps in which the additions take place, for me? (define (fib n) (if (<= n…
Pradit
  • 709
  • 5
  • 12
  • 27
7
votes
6 answers

Fibonacci sequence in Javascript

I am very new to programming in general and am having a hard time understanding this Fibonacci sequence example: var fib = [0, 1]; for (var i = 2; i < n; i++) { fib[ i ] = fib[ i - 1 ] + fib[ i - 2 ]; console.log(fib); } On the first…
KMcA
  • 1,155
  • 1
  • 11
  • 21
7
votes
2 answers

Why does memoization not work?

After reading a memoization introduction I reimplemented the Fibonacci example by using a more general memoize function (only for learning purposes): memoizer :: (Int -> Integer) -> Int -> Integer memoizer f = (map f [0 ..] !!) memoized_fib :: Int…
Bastian
  • 4,638
  • 6
  • 36
  • 55
6
votes
3 answers

How do I write this Clojure function so that it doesn't blow out the stack?

I'm new to Clojure and I think my approach to writing code so far is not in line with the "Way of Clojure". At least, I keep writing functions that keep leading to StackOverflow errors with large values. I've learned about using recur which has been…
bitops
  • 4,022
  • 3
  • 26
  • 31
6
votes
2 answers

Fibonacci Search

Somebody please explain me the fibonacci search algorithm. I have tried numerous resources around and searched a lot, but the algorithm is still unclear. Most of the resources described it in link with binary search, but I didn't understand 'em. I…
Nilesh
  • 624
  • 1
  • 7
  • 23
6
votes
5 answers

What is wrong with this Fibonacci function?

Stumbled upon this example of bad C++ code in a blog post, without any explanation as to why it is considered "bad". I have my own ideas, but would like to hear experienced C++ devs on this. unsigned int Fibonacci (unsigned int n) { if (n == 0…
Egor Pavlikhin
  • 17,503
  • 16
  • 61
  • 99