Func1(n) 1. s ← 0; 2. for i ← 2n to 3n do 3. for j ← 1 to n⌊log2 n⌋ do 4. for k ← 2j+5…
- User
Func1(n)
- s ← 0;
- for i ← 2n to 3n do
- for j ← 1 to n⌊log_2 n⌋ do
- for k ← 2j+5 to 2j+8 do
- s ← s + i - j + k;
- end
- end
- end
- 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)
- s ← 0;
- for i ← 1 to 10⌊n^0.5⌋ do
- for j ← 1 to i^2 do
- s ← s + i - j;
- end
- end
- 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)
- s ← 0;
- for i ← 1 to 2n^4 do
- for j ← 5i^2+1 to 7i^2 do
- for k ← 3 to 10 do
- s ← s + i - j + k;
- end
- end
- end
- 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)
- s ← 0;
- for i ← 2 to 2n^3 do
- for j ← 3 to 4i^2⌊log_2 i⌋ do
- s ← s + i - j;
- end
- end
- 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)
- s ← 0;
- Assistant
As you have provided, we can denote the time complexities of the functions as follows:
Func1(n)
For Func1, you have calculated it correctly. We have the total running time as: T(n) = n * n⌊log_2 n⌋ * 4 Simplifying it, the complexity is indeed O(n^2 log n) because the ⌊log_2 n⌋ term has a slower growth rate than the n^2 term, and the constant factor can be omitted in big-O notation.
Func2(n)
For Func2, your explanation is also correct. The sum of squares of the first numbers up to 10⌊n^0.5⌋ can be approximated as O(n), due to the squaring of a square root of n (i.e., 10⌊n^0.5⌋^2 is linear in the order of n).