To find the running time of these algorithms, we will analyze the loops and determine their bounds through tables and sums. Since we are interested in the asymptotic behavior, we can ignore constant terms and lower-order terms when appropriate.
Func2
Let's start with Func2. The while loop depends on j starting from 7n^2 and getting divided by 9 at each iteration until it drops below 9. Let's make a table to see the values of j:
Iteration | j Value
1 | 7n^2
2 | 7n^2 / 9
3 | (7n^2 / 9) / 9
...
k | 7n^2 / 9^k
To find out for how many iterations the loop runs, we can solve for when j falls below 9:
7n^2 / 9^k < 9
7n^2 < 9^(k+1)
n^2 < 9^k / 7
n^2 < (3^2)^k / 7
n^2 < (3^(2k)) / 7
log(n^2) < 2k*log(3) - log(7)
2log(n) < 2k*log(3)
log(n) < k*log(3)
log(n) / log(3) < k
log_3(n) < k
The loop will stop when k is just above log_3(n), this is the upper bound is O(log n).
For the lower bound, we can see that the loop will certainly iterate at least k=log_3(n) times, because it takes that many divisions by 9 to make 7n^2 approximately n. Thus, the lower bound is Ω(log n).
The upper and lower bounds are both logarithmic in n, and they differ by at most a constant factor — therefore, we can conclude that the runtime of Func2 is Θ(log n).
Func3
Next, let's look at Func3. We have a nested loop where the outer loop exponentially increases i by a factor of 4, starting from n and ending before 5n^3. The inner loop is similar to the Func2 loop, which has a complexity of Θ(log n). Let's first analyze the outer loop:
Iteration | i Value
1 | n
2 | 4n
3 | 4^2 * n
...
t | 4^t * n
To find out the number of iterations for the outer loop, we solve for i when it is just below 5n^3:
4^t * n < 5n^3
4^t < 5n^2
t*log(4) < log(5) + 2*log(n)
t < (log(5) + 2*log(n)) / log(4)
We are only interested in the dominant n term for big O notation, so t is approximately 2 * log_4(n). The outer loop runs O(log n) times.
For the inner loop, it has been established to have Θ(log n) runtime, hence the nested loop would be O((log n)*(log n)).
For the lower bound, similar reasoning shows that the runtime is Ω((log n)*(log n)).
Since the upper and lower bounds differ only by a constant factor and are both logarithmic in n, the overall complexity of Func3 is Θ((log n)^2).
Func4
Now let's evaluate Func4. Here, the outer loop is a simple for loop that runs 5n times. The inner loop multiplies j by 5 each time until it is larger than i^3. Let's analyze the inner loop:
Iteration | j Value
1 | 3i
2 | 3i * 5
3 | 3i * 5^2
...
m | 3i * 5^m
For the iterations to stop, we must have j >= i^3, which simplifies to:
3i * 5^m >= i^3
5^m >= i^2 / 3
m*log(5) >= 2*log(i) - log(3)
m >= (2*log(i) - log(3)) / log(5)
Again, considering only the dominant term for asymptotic behavior, m is approximately 2*log(i). Since i goes up to 5n, the inner loop runs no more than O(log n) times per each iteration of the outer loop.
The overall complexity is a product of the complexities of the outer and inner loops: O(5n * log n). Since constant factors can be ignored in big O notation, we say that the runtime complexity of Func4 is O(n log n).
For the lower bound, we can argue that the inner loop runs at least log(n) times for each iteration of the outer loop (if we consider a similar analysis with slight adjustment to the inequalities), and the outer loop runs n times. This gives us a lower bound of Ω(n log n).
Therefore, the analysis shows the running time of Func4 to be Θ(n log n).