695 views
4 4 votes

Let $T(n)$ denote the number of times the for loop in below code is executed on any input $n$. What can be said about $T(n)$?

int iscompute(int n) 
{
    for (int i=2;i<=sqrt(n); i++) 
        if(n%i) == 0) 
        { 
            printf("not computed"); 
            return 0; 
        } 
    return 1;
}
  1. $T(n)= O(\sqrt n)$ and $T(n)= \Omega(\sqrt n)$
  2. $T(n)= O(\sqrt n)$ and $T(n)= \Omega(1)$
  3. $T(n)= O( n)$ and $T(n)= \Omega (\sqrt n)$
  4. None

1 Answer

Best answer
6 6 votes
int iscompute(int n) 
{
    for (int i=2;i<=sqrt(n); i++) 
        if(n%i) == 0) 
        { 
            printf("not computed"); 
            return 0; 
        } 
    return 1;
}

For loop will run sqrt(n) times at worst when $n$ is a prime number. if n is any even number then if condition becomes true in 1st iteration only. So it will terminate after that.This will result Constant best case complexity.

• selected by
Answer:
Position:
Show:

Related questions

3 3 votes
1 answers 1 answer
1.2k
1.2k views
Bikram asked Oct 4, 2016
1,153 views
Let we have 3 steps of an arbitrary program fragment whose running times are $O(n^2), \: O(n^3)$ and $O(n\log n)$, then the running time of whole program is$O(n^3)$$\Omeg...
2 2 votes
1 1 answer
828
828 views
Bikram asked Oct 4, 2016
828 views
Order the following functions by growth rate :$\log n$$n/\log n$$(3/2)^n$$n\log^2 n$a. $\log n$ $\quad$ b. $n/\log n$ $\quad$ d. $n\log^2 n$ $\quad$ c. $(3/2)^n$ d. $n\...
2 2 votes
1 answers 1 answer
688
688 views
Bikram asked Oct 4, 2016
688 views
The Matrix Chain-Product dynamic programming Algorithm runs in _______linear timeexponential timequadratic timecubic time
1 1 vote
1 answers 1 answer
821
821 views
Bikram asked Oct 4, 2016
821 views
Match the following two columns given in a table:1. Randomized quick sorta. $\Theta(n+k)$2. Insertion sortb. $\Theta\left(n^2\right)$3. selection sortc. $\Theta(n)$4. Buc...