recategorized by
817 views
1 votes
1 votes
for(i=1 to n)
    {
        if(n mod i==0)
        {
            for(int j=1 to n)
            printf(j);
        }
    }
recategorized by

2 Answers

Best answer
6 votes
6 votes
$T(n) = \Omega(n)$, when $n$ is a prime number.

$T(n) = O\left(n\sqrt n \right)$, when $\sqrt n$ is a factorial.
edited by
0 votes
0 votes
The nested loop runs only in case of divisor of N, whenever i dividies N , the nested loop runs N times,

As divisor of any number are not equivalent to N , they may be less then sqrt(N)

And to find number of divisors of any number, it will take sqrt(N) complexity.

So overall lets say N have sqrt(N) divisors

Hence as per my knowledge , the overall complexity, of the above code is sqrt(N) * N

Not N^2.

Best complexity is o(N)  in case of primes number, when N = prime , it have only two divisors in that case complexity is linear.

Related questions

0 votes
0 votes
0 answers
1
pbhati asked Jun 12, 2018
338 views
What is the time complexity of the following piece of code in the terms of n?$Main()${$n=2^{2^k};$$for(i=1; i<=n; i++)${ $j=2;$ $while(j<=n)$ { $j=j^2;$ }}...
2 votes
2 votes
0 answers
2
junaid ahmad asked Jan 22, 2018
449 views
void fun(intn) { int s=0; for(i=1;i<=n;i++) { for(j=1;j<=i*i;j++) { if(j%i==0) { for(k=1;k<=j;K++) s++ } } } }
5 votes
5 votes
1 answer
3
rahul sharma 5 asked Oct 18, 2017
698 views
T(n) = 4T(n/2) + n2.$\sqrt{2}$In thetha notation?