Provide a tight Θ bound on the running time of the function of n.
a = n
while ( a > 1 or a > 2 or a > 3)
for b = 1 to n
print "Hello World"
a = sqrt(a)
Would the running time be dependent on the condition set in the while loop? Currently my answer for this is nlog(n) but I am not sure if that is correct? Can someone clarify? I thinking the sqrt makes it log(n)
The first inner loop (the one based on
for b = 1 to n) usesnsteps. Thennis replaced bysqrt(n)hence the second inner loop usessqrt(n)steps, and so on. This shows the run time isn + sqrt(n) + sqrt(sqrt(n)) + ...If
n = 2^(2^k)thensqrt(n) = 2^(2^(k-1)),sqrt(sqrt(n)) = 2^(2^(k-2)), and so on, hence there arekterms in the sum above. Thus, for some generaln, there are aboutlog(log(n))terms. Counting the first loop separately, one sees that the run time is more thannand less thann + sqrt(n) * log(log(n)). Finally, the run time is equivalent ton, in particular it isΘ(n).