You are on page 1of 6

Elżbieta Sak, 205077

Algorithms & Computability


Report

1 Problem formulation
The aim of the program is to calculate and . Since we do not know the
n! nn
size of , we cannot use built types like unsigned integers or long integers
n
to store results of calculations. This is due to limitation of these built types
that is if the result is more than maximum integer number (depends on the
system architecture), it becomes a very small integer. That is why it is
better to use other ways for storing big numbers.

Moreover, we would like to calculate the number of elementary operations,


estimate the time of execution of the algorithms calculating numbers
mentioned before. In addition the task will include also calculating the
number of elementary operations of a sorting algorithm. In my case it will
be insertion sort.

It seems that the numbers of elementary operations are some values of a


polynomial function. Let us assume it is a polynomial of degree 3 and we
will try to calculate coefficients of this function. However, it may happen
that we will be able to find some more suitable function that fits the
values. It will turn out after a detailed analysis.

2 Solution for big numbers arithmetic


The program will be written in C++ in order to reduce the time needed for
execution and simplify memory access. The number will be predefined
n
and stored in a text file. During program execution every digit of will be
n
added to the stack (implemented as a class with double linked list). The
program will use double linked-lists to store all the digits. Hence, the
addition and multiplication will be performed iteratively on 2 double-linked
lists (arguments of functions). The main functionality will be ensured by a
container called bigNum which has a structure for storing digits (it is a
stack implemented as a double-linked list where the least significant digits
are at the top and the most significant ones are at the bottom) and

1/6
methods for operating on digits (reading and creating lists of digits,
addition, multiplication).

3 Application of big numbers arithmetic to basic


tasks

Data Representation:

bigNum

struct digit {
int d;
digit *next;
digit *prev;
};

digit *head;
digit *tail;

bool isEqual(bigNum *num);


void insertDigit(int d);
void insertDigitLast(int d);
void addNumbers(bigNum *num);
bigNum *mulNumbers(bigNum *num);
void readFromFile(char* filename);
void saveToFile(char* filename);
void printBigNum();

Since double-linked lists contain integers, the addition and multiplication


will be performed as usual standard written methods.

The assumption of algorithms is that the first number is greater or equal to


the second one.

2/6
Addition:

Fig 1 Flowchart: Addition

We start addition from taking two bigNum numbers N1 and N2. Then we
take the first (the least significant) digits and define them as N1digit and
N2digit. We also define result(= 0) and initialize a carry to 0 - a flag
denoting that the sum of two digits is greater than 9 (0 – means no
correction is needed, 1 – correction is needed). In the loop (until the end of
the first number is reached) we calculate the result of adding 2 digits using
a formula : result = carry + N1digit + N2digit. If the result is greater than 9
we set carry to 1 and change the result to result – 10, otherwise carry is
set to 0. Then we change the current digit of N1 to result. If N1 does not
have the next digit and carry = 1, we add 1 to N1 (we add 1 as the most
significant digit). In the case of reaching the end of N2 and carry = 0 we
end addition, otherwise we continue the algorithm.
3/6
Multiplication:

Fig 2 Flowchart: Multiplication

Multiplication starts from taking two numbers of type bigNum N1 and N2


respectively. The next step is definition of N2digit – the first (the least
significant) digit of N2, result – the result of multiplication N1 by N2,
currentSum – the result of multiplying N1 by every digit of N2 ( = 0). The
first (outer) loop checks if we reached the last (the most significant) digit of
N2. If so we return the result and end multiplication, otherwise we start
from initialization of currentSum by 0’s. The number of 0’s depend on the
position of current N2digit counting from the least significant digit - if the
position of N2digit is 1 we do not put any 0’s, if the position of N2digit is 2
we put one 0, if the position of N2digit is 3 we put two 0’s, if the position of
N2digit is 4 we put three 0’s and so on. Then we initialize N1digit to the
first digit of N1 and carry to 0. The second (inner) loop (checking if we read

4/6
the last digit of N1) we compute the result of multiplication of 2 digits and
store it in a variable n: n = carry + (N1digit * N2digit). If n is greater than
9, carry is set to n / 10 (integer division) and n % 10 is added to
currentSum (we add n to the current number). If we reach the end of N1,
we add carry to currentSum. Otherwise if n is less or equal to 9, we set
carry to 0 and add n to currentSum. Then we take the next digit of N1 go
again to the second loop. When we reach the end of N1 we check if the
variable result stores any value. If not we set result to currentSum,
otherwise the result is set to the sum of result and CurrentSum. Then we
take the next digit of N2 and go to the first loop.

4 Methods of definition of input size


The input size of the argument of functions will be the value of stored in
n
the file. it will be an integer.

The input size of a sequence to be sorted will be the length of this


sequence. The sequence will be an array predefined in the program.

5 Definition of elementary operations


The program will take into account:

- access to memory

- addition

- assignment

- comparison

- memory allocation

- memory deallocation

- multiplication

6 Uniform and logarithmic criteria


The complexity of both algorithms will be calculated using uniform criteria,
since these criteria are better for arguments stored as integers (instead of
binary representations).

7 Format of results
In case of the results of algorithms, they will be stored in a file. Moreover,
there will be a simple GUI to represent these results.

5/6
The numbers of elementary operations will be exported to a csv file.

8 Approximation of experimental results


The results will be values of some polynomial function and we will try to
approximate them to fit a polynomial of degree 3 (if it possible). If it is not
possible, we will try to find another function that fits better.

9 Illustration of results
The program will be chosen later (maybe Excel, Matlab or R).

6/6

You might also like