4

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 Θ for this for loop,
I think it's square-root(n) but is there an order named square root n?

My answer is:
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!

Saeed Amiri
  • 22,252
  • 5
  • 45
  • 83
  • 2
    @JackKrauser To have this question fit better into the Q&A model (as "Your analysis is correct" doesn't make for a particularly good answer), I suggest you change this question into asking something like "What's the running time complexity of this loop?" and then post an answer with your analysis, which you then accept. – Bernhard Barker Oct 18 '13 at 15:22

1 Answers1

1

O(sqrt n) looks weird but right to me

evhen14
  • 1,839
  • 12
  • 16