retagged by
935 views
0 votes
0 votes
int isprime(int n )
{
for(int i=2;i<=sqrt(n) ; i++)
{
if(n%i==0)
{
not prime
}
}
retagged by

1 Answer

2 votes
2 votes

I think tightest lower bound is  Ω(1) . 

if you input is say 2 or 3  then it will i<=sqrt(n) become false and exit .  and its worst case  O(n^0.5)

this prog will be more efficient if there is break statement in the if part  . 

int isprime(int n )
{
for(int i=2;i<=sqrt(n) ; i++)
{
if(n%i==0)
{
not prime
break ;

}
}

Related questions

0 votes
0 votes
2 answers
2
radha gogia asked Jul 7, 2018
1,587 views
foo(int n) { for(int i=0 ; i<n ;i++) for(int j=i ; j<=i*i ;j++) if(j%i==0) { for(int k=0;k<j;k++) printf("hii"); } } How to proceed here for analyzing the time complexity...
1 votes
1 votes
2 answers
4
vaishali jhalani asked Nov 5, 2016
1,012 views
What is the meaning of upper bound and worst case lower bound here?