Pset 1
Pset 1
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.
f(n)
log(n)
n log(n)
n2
2n
n!
Problem 3: Express each function in terms of big O notation and determine its
complexity class (the symbol ^ means power)
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).
}