University of Mascara Algorithmic and Complexity
Department of Computer sciences
Second Year Engineer
Problem Set 1
Problem 1
For each function f(n) and time t in the following table, determine the largest size n of a
problem that can be solved in time t, assuming that the algorithm to solve the problem
takes f(n) microseconds.
t 1 second 1 minute 1 hour 1 day 1 month 1 year
f(n)
log(n)
n log(n)
n2
2n
n!
Problem 2: Arrange the following functions in ascending order of complexity
n, 2000, √ , log2 (n), n√ , log (log(n)) , √ log(n), n/log(n)
Problem 3: Express each function in terms of big O notation and determine its
complexity class (the symbol ^ means power)
5n + 3, 3n^2 + 2n – 1, 2n^3 + 4n^2 - 7n, 10n^2 + 8n + 12, 2^n + 3n, n!,
n^4 + 2n^3 + n^2 + n + 1, log(2n+1), √5n+1, (5n+1) log(n/2), 4n^5 - 3n^3+7n,
2^n + n^4, n^(log(3)), 2n^2 + 3n log2(n), n^(n+1)
1
Problem Set 1
Problem 4: int function6 (int n) {
int r, d;
d = 1;
Find the complexity of the below functions r = 0;
int function0(int n) { while (d * d <= n) {
int sum = 0; d = d * 2;
for (int i = 1; i <= n; i++) { }
sum += i;
} while (d > 1) {
return sum; d = d / 2;
} if ((r + d) * (r + d) <= n) {
void function1(int n) r = r + d;
{ }
for (int i=1; i<n; i=i*2) }
printf(“Marhaba”); return r;
} }
int function2(int n)
{ void Function7(int n, int m, int *q, int
int c = 0; *r) {
for (int i=n/2; i<=n; i++) *q = 0;
for (int j=1; j<=n; j++) *r = n;
c+=i+j; while (m <= *r) {
return c ; (*q)++;
} *r -= m; }
void function3(int n) }
{
int count = 0;
for (int i=1; i<=n/3; i++) Problem 5:
for (int j=n/2; j<=n; j++)
for (int k=1; k<=n; k Give two functions with different complexities to
= k * 2)
count++; check if an integer n is prime number
}
void function4(int n) Problem 6
{
int count = 0;
Give a function to count the number of digits in a
for (int i=n/2; i<=n; i++)
for (int j=1; j+n/2<=n; j = positive integer, then estimate its complexity
j++)
for (int k=1; k<=n; k
= k * 2) Problem 6
count++;
} Propose programs for the following problems,
void function5(int n)
{ and then calculate their complexities:
int i = 1, s =1;
while (s <= n) Rotate an n x n matrix in the clockwise
{ Transpose an n x m matrix A into an m x
i++; n matrix B.
s += i;
printf("*"); Multiply two matrices A (n x m) and B
} (m x p) to get C (n x p).
}