Cycle time to i * i <= n

Here is the code:

int foo(int n)
{
    if(n == 1)
        return 1;
    int f = 0;
    int i;
    for(i=1; i*i<=n; i++)
        if(n%i == 0)
            f+=2;
    i--;
    if(i*i == n)
        f--;
    return f;
}

My problem is that I cannot determine & Theta; for this to loop,
I think it is the square root (n), but is there an order called square root n?

My answer:
Theta (sqrt (n)) because of this loop

for(i=1; i*i<=n; i++) 

i * i <= n take sqrt for both sides

i <= sqrt(n)

Correct me if I am wrong!

+4
source share
1 answer

O (sqrt n) looks weird but right to me

+1
source

All Articles