You are on page 1of 8

Ministery of Education of the Republic of Moldova

Technical University of Moldova

REPORT
Laboratory work nr.1
Complexity and Calculability

FAF-181 Belinschi Victoria

Checked By: M. Catruc

Chisinau 2020
Tema: Algorithm Analysis

The purpose of the work:

1. Empirical analysis of the algorithms

2. Teoretical analysis of the algorithms

3. Determination of the temporal and asymptotic complexity of the algorithms

Task for Work # 1: Fibonacci Numbers


The Fibonacci string is defined by the following recurrence:

This famous string was discovered in 1202 by Leonardo Pisano (Leonardo of Pisa), known as
Leonardo Fibonacci. The nth term of the string can be obtained directly from the definition:

function fib1(n)

if n < 2 then return n

else return fib1(n-1) + fib1(n-2)
This method is very inefficient, because it recalculates the same values several times. Here's
another, more efficient method that solves the same problem.

function fib2(n)

i ← 1; j ← 0

for k ← 1 to n do j ← i + j

i ← j - i

return j

Another algorithm:
function fib3(n)

i ← 1; j ← 0; k ← 0; h ← 1

while n > 0 do

if n este impar then t ← jh

j ← ih+jk+t

i ← ik+t

t ←h 
 2

h ← 2kh+t

k ← k2+t

n ← n div 2

return j
Basic task:
Perform the empirical analysis of the proposed algorithms.
Determine the relationship that determines the temporal complexity for these algorithms.
Determine the asymptotic complexity of the algorithms.
Make a conclusion about the work done.

Algorithm Description

Algorithm is the fundamental notion of computer science. Everything is built around algorithms
(and data structures, such as lists or graphs). It is also a fundamental concept of modern
mathematics alongside that of axiomatic system

The execution time of the algorithms

Often, in order to solve a problem, an algorithm has to be chosen from several possible ones, two
main criteria of choice being contradictory: the algorithm is simple to understand, to code and to
debug; the algorithm to use the computer resources efficiently, to have a short execution time. If
the program to be written needs to be run a small number of times, the first requirement is more
important; In this situation, the time to set up the program is more important than its running
time, so the simplest version of the program must be chosen. If the program is to be run a large
number of times, with a large number of data to be processed, the algorithm that results in faster
execution must be chosen. Even in this situation, the simpler algorithm should be implemented
earlier and the runtime reduction that the complex algorithm implementation would bring should
be calculated.

Empirical analysis of the complexity of algorithms

An alternative to the mathematical analysis of complexity is represented by the empirical


analysis. This can be useful for:

- obtaining preliminary information about the complexity class of an algorithm;

- to compare the efficiency of two (or more) algorithms for solving the same problem;

- - to compare the efficiency of several implementations of the same algorithm;

- - to obtain information on the efficiency of implementing an algorithm on a particular computer


Program code in C++
#include <iostream>
#include <time.h>
#include <conio.h>
using namespace std;
unsigned long long fib_1(int n);
unsigned long long fib_2(int n);
unsigned long long fib_3(int n);
unsigned long long num_schmb_1 = 0;
unsigned long long num_schmb_2 = 0;
unsigned long long num_schmb_3 = 0;
int main() {
clock_t start_1, finish_1;
clock_t start_2, finish_2;
clock_t start_3, finish_3;
int n;
int choice = 0;
unsigned long long fib1;
unsigned long long fib2;
unsigned long long fib3;
cout << "Ce numar din sirul lui Fibonacci doriti sa aflati: ";
cin >> n;
while (true) {
cout << endl;
cout << "1. Algoritmul recursiv" << endl;
cout << "2. Algoritmul iterativ" << endl;
cout << "3. Algoritmul divizarii" << endl;
cout << "0. Exit" << endl;
cout << "Optiunea: ";
cin >> choice;
if (choice == 1) {
start_1 = clock();
fib1 = fib_1(n);
finish_1 = clock() - start_1;
cout << "\n\nUtilizind algoritmul 1" << endl;
cout << "Numarul lui fibonacci este: " << fib1 << endl << "S-au
produs "
<< num_schmb_1 << " interschimbari in " <<
(float)finish_1 / CLOCKS_PER_SEC << " secunde." << endl;
}
if (choice == 2) {
start_2 = clock();
fib2 = fib_2(n);
finish_2 = clock() - start_2;
cout << "\nUtilizind algoritmul 2" << endl;
cout << "Numarul lui fibonacci este: " << fib2 << endl << "S-au
produs "
<< num_schmb_2 << " interschimbari in " <<
(float)finish_2 / CLOCKS_PER_SEC << " secunde." << endl;
}
if (choice == 3) {
start_3 = clock();
fib3 = fib_3(n);
finish_3 = clock() - start_3;
cout << "\nUtilizind algoritmul 3" << endl;
cout << "Numarul lui fibonacci este: " << fib3 << endl << "S-au
produs "
<< num_schmb_3 << " interschimbari in " <<
(float)finish_3 / CLOCKS_PER_SEC << " secunde." << endl;
}
if (choice == 0){ return 0; }
}
}
unsigned long long fib_1(int n) {
if (n <= 2) return 1 ;
num_schmb_1++;
return fib_1(n - 1) + fib_1(n - 2);
}
unsigned long long fib_2(int n) {
unsigned long long fib_num = 0;
unsigned long long k = 1;
for (int i = 0; i < n; i++) {
fib_num = k + fib_num;
k = fib_num - k;
num_schmb_2 ++;
}
return fib_num;
}
unsigned long long fib_3(int n) {
unsigned long long i = 1;
unsigned long long j = 0;
unsigned long long k = 0;
unsigned long long h = 1;
unsigned long long t;
while (n > 0) {
if ((n % 2) != 0) {
t = j*h;
j = (i*h) + (j*k) + t;
i = (i*k) + t;
num_schmb_3++;
}
t = h*h;
h = (2 * k*h) + t;
k = (k*k) + t;
n = n / 2;
num_schmb_3 ++;
}
return j;

}
Screen

Results

Nr. 
 
 
 

n=10 n=20 n=30 n=40
Alg.
Iterations:54 Iteration:6764 Iteration: 832039 Iteration: 102334154
1
Time:0.003 Time:0.003 Time: 0.078 Time: 9.29

Iteration:10 Iteration: 20 Iteration: 30 Iteration: 40


2
Time:0 Time: 0 Time: 0 Time: 0
Iteration:6 Iteration:7 Iteration:9 Iteration: 8
3
Time:0 Time:0 Time: 0 Time: 0

Use of Fibonacci series


Fibonacci can be found in nature not only in the famous rabbit experiment, but also in beautiful
flowers (Internet access, 12). On the head of a sunflower and the seeds are packed in a certain
way so that they follow the pattern of the Fibonacci sequence. This spiral prevents the seed of the
sunflower from crowding themselves out, thus helping them with survival. The petals of flowers
and other plants may also be related to the Fibonacci sequence in the way that they create new
petals (Internet access, 10).

2.1 Petals on flowers

Probably most of us have never taken the time to examine very carefully the number or
arrangement of petals on a flower. If we were to do so, we would find that the number of petals
on a flower that still has all of its petals intact and has not lost any, for many flowers is a
Fibonacci number (Internet access,8).

• 1 petal: white cally lily

• 3 petals: lily, iris



• 5 petals: buttercup, wild rose, larkspur, columbine (aquilegia)

• 8 petals: delphiniums

• 13 petals: ragwort, corn marigold, cineraria,

• 21 petals: aster, black-eyed susan, chicory

• 34 petals: plantain, pyrethrum

• 55, 89 petals: michaelmas daisies, the asteraceae family (Internet access,19)

Fibonacci in Music

The Fibonacci sequence of numbers and the golden ratio are manifested in music widely.

The numbers are present in the octave, the foundational unit of melody and harmony.

Stradivarius used the golden ratio to make the greatest string instruments ever created.

Howat’s( 1983) research on Debussy’s works shows that the composer used the golden ratio and

Fibonacci numbers to structure his music. The Fibonacci Composition reveals the inherent
aesthetic appeal of this mathematical phenomenon. Fibonacci numbers harmonize naturally and
the exponential growth which the Fibonacci sequence typically defines in nature is made present
in music by using Fibonacci notes. The intervals between keys on a piano of the same scales are
Fibonacci numbers (Gend, 2014).

Conclusion
As a result of this laboratory work we analyzed 3 different methods of calculating the Fibonaci
string. After running the program we came to the conclusion that different algorithms have
different execution time. All methods work but they differ by efficiency. The execution time is
very small and the number of iterations for large numbers does not increase very quickly.

You might also like