0% found this document useful (0 votes)
220 views2 pages

11 Fibonacci

The document discusses Fibonacci numbers and algorithms for calculating them. It defines the Fibonacci sequence recursively, where each number is the sum of the previous two, starting with 0 and 1. It then presents two algorithms: a recursive one that is inefficient, and a dynamic programming approach that is O(n) time by storing the previously calculated numbers. Faster O(log n) algorithms are mentioned using matrix multiplication and a closed-form formula. An exercise is given to check if numbers can be expressed as the sum of two Fibonacci numbers, which can be done in O(n+m) time by considering all pairs of Fibonacci numbers smaller than the numbers.

Uploaded by

Dung Quoc Van
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
220 views2 pages

11 Fibonacci

The document discusses Fibonacci numbers and algorithms for calculating them. It defines the Fibonacci sequence recursively, where each number is the sum of the previous two, starting with 0 and 1. It then presents two algorithms: a recursive one that is inefficient, and a dynamic programming approach that is O(n) time by storing the previously calculated numbers. Faster O(log n) algorithms are mentioned using matrix multiplication and a closed-form formula. An exercise is given to check if numbers can be expressed as the sum of two Fibonacci numbers, which can be done in O(n+m) time by considering all pairs of Fibonacci numbers smaller than the numbers.

Uploaded by

Dung Quoc Van
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Chapter 11

Fibonacci numbers
The Fibonacci numbers form a sequence of integers defined recursively in the following way.
The first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number
is the sum of the previous two.

Fn =

+ Fn2

for n = 0,
for n = 1,
for n > 1.

n1

The first twelve Fibonacci numbers are:


0

13

21

34

55

89

10

11

Notice that recursive enumeration as described by the definition is very slow. The definition
of Fn repeatedly refers to the previous numbers from the Fibonacci sequence.
11.1: Finding Fibonacci numbers recursively.
1
2
3
4

def fibonacci(n):
if (n <= 1):
return n
return fibonacci(n - 1) + fibonacci(n - 2)

The above algorithm performs Fn additions of 1, and, as the sequence grows exponentially,
we get an inefficient solution.
Enumeration of the Fibonacci numbers can be done faster simply by using a basis of
dynamic programming. We can calculate the values F0 , F1 , . . . , Fn based on the previously
calculated numbers (it is sufficient to remember only the last two values).
11.2: Finding Fibonacci numbers dynamically.
1
2
3
4
5
6

def fibonacciDynamic(n):
fib = [0] * (n + 2)
fib[1] = 1
for i in xrange(2, n + 1):
fib[i] = fib[i - 1] + fib[i - 2]
return fib[n]

The time complexity of the above algorithm is O(n).


Copyright 2013 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure
prohibited.

11.1. Faster algorithms for Fibonacci numbers


Fibonacci numbers can be found in O(log n) time. However, for this purpose we have to use
matrix multiplication and the following formula:
"

1 1
1 0

#n

"

F
Fn
, for n 1.
= n+1
Fn Fn1

Even faster solution is possible by using the following formula:

( 1+2 5 )n ( 12 5 )n

Fibn =
5

(11.1)

These algorithms are not trivial and it will be presented in the future lessons.

11.2. Exercise
Problem: For all the given numbers x0 , x1 , . . . , xn1 , such that 1 xi m 1 000 000,
check whether they may be presented as the sum of two Fibonacci numbers.
Solution: Notice that only a few tens of Fibonacci numbers are smaller than the maximal
m (exactly 31). We consider all the pairs. If some of them sum to k m, then we mark
index k in the array to denote that the value k can be presented as the sum of two Fibonacci
numbers.
In summary, for each number xi we can answer whether it is the sum of two Fibonacci
numbers in constant time. The total time complexity is O(n + m).

Every lesson will provide you with programming tasks at http://codility.com/train.

You might also like