Let's start by analyzing each function one by one.
Func1(n)
1. s ← 0;
2. for i ← 2n to 3n do
3. for j ← 1 to n⌊log_2 n⌋ do
4. for k ← 2j+5 to 2j+8 do
5. s ← s + i - j + k;
6. end
7. end
8. end
9. return (s);
The first loop (line 2) runs from 2n to 3n, so it has n iterations. The second loop (line 3) runs from 1 to n⌊log_2 n⌋, so it has n⌊log_2 n⌋ iterations. The third loop (line 4) runs from 2j+5 to 2j+8, which is 4 iterations, since it's independent of n. Therefore, the total running time is:
T(n) = n * n⌊log_2 n⌋ * 4
We can simplify this by removing the floor function since it only affects lower order terms:
T(n) ≈ n^2 * log_2(n) * 4
The complexity is O(n^2 log n).
Func2(n)
1. s ← 0;
2. for i ← 1 to 10⌊n^0.5⌋ do
3. for j ← 1 to i^2 do
4. s ← s + i - j;
5. end
6. end
7. return (s);
The first loop (line 2) runs for 10⌊n^0.5⌋ iterations. The second loop (line 3) depends on i and runs for i^2 iterations. We can represent it as:
T(n) = ∑[i=1 to 10⌊n^0.5⌋] ∑[j=1 to i^2] 1
Simplifying, we get a series summing the squares of the first 10⌊n^0.5⌋ natural numbers. The upper bound is (10n^0.5)^2, so the overall complexity is O(n).
Func3(n)
1. s ← 0;
2. for i ← 1 to 2n^4 do
3. for j ← 5i^2+1 to 7i^2 do
4. for k ← 3 to 10 do
5. s ← s + i - j + k;
6. end
7. end
8. end
9. return (s);
The first loop (line 2) runs 2n^4 times. The second loop (line 3) is based on i^2 and runs for roughly 2i^2 iterations (7i^2 - 5i^2). The third loop (line 4) has a constant number of iterations (8).
T(n) = ∑[i=1 to 2n^4] ∑[j=5i^2+1 to 7i^2] 8
Simplifying, the inner sum is a constant times the square of i (2i^2), so the total is:
T(n) ≈ 8 * ∑[i=1 to 2n^4] 2i^2
The summation of i^2 from 1 to 2n^4 leads to a complexity O(n^8).
Func4(n)
1. s ← 0;
2. for i ← 2 to 2n^3 do
3. for j ← 3 to 4i^2⌊log_2 i⌋ do
4. s ← s + i - j;
5. end
6. end
7. return (s);
The first loop (line 2) runs for 2n^3 iterations. The second loop (line 3) depends on i^2 and the logarithm of i. Even though we have a logarithm, the i^2 term will dominate. Assume the upper bound for the inner loop is O(i^2⌊log_2 i⌋), which is O(i^2 log i) because i^2 grows faster than log i.
T(n) = ∑[i=2 to 2n^3] O(i^2 log i)
This is not trivial to simplify, but an approximation considering that i^2 log i is the dominating term and log i changes slower than i, we might assume it behaves similar to:
T(n) ≈ O(n^9 log n)
The complexity appears to be O(n^9 log n).
Func5(n)
1. s ← 0;
2. for i ← n to 2n^2-1 do
3. for j ← 1 to i^3 do
4. s ← s + i - j;
5. end
6. end
7. return (s);
The first loop (line 2) runs from n to 2n^2, so it has roughly n^2 iterations. The second loop runs for i^3 iterations for each i. We express this as:
T(n) = ∑[i=n to 2n^2] i^3
This summation roughly behaves like the sum of cubes of the first 2n^2 natural numbers, so the dominant term is proportional to the fourth power of the upper limit, giving a total complexity:
T(n) ≈ O((2n^2)^4) = O(16n^8)
Simplifying the constant, the complexity is O(n^8).