-3

I would like to determine the asymptotic complexity in THE WORST CASE the following function:

int j;
float r = 1.0;
for (int i=1; i<(log n); i++){
    j = 1;
    while (j <= i^2){
        r*=2;
        j++;
}
print(r);
CollinD
  • 7,304
  • 2
  • 22
  • 45
  • 5
    That'd be tough since this code won't compile with the unclosed `{` on line 3, and the `log n`. I'd suggest either sticking to some flavor of pseudocode, or 100% real c++ for clarity. Further, what have you tried thus far to determine the time complexity? SO isn't a homework-answering service. – CollinD Jul 22 '16 at 09:11
  • You'll get a float overflow if n >= 1000 – Thomas Ayoub Jul 22 '16 at 09:20

1 Answers1

2

Firstly, I'll assume that i^2 in your code means "i raised to the power of 2", rather than "i bitwise-XOR 2", as the latter is consistent with C++ syntax, but produces unpredictable results.

Time complexity is given by the sum

![enter image description here

We need to evaluate the sums of natural numbers to the power of 2, using info from this webpage: http://www.trans4mind.com/personal_development/mathematics/series/sumGeneralPowersNaturalNumbers.htm.

enter image description here

So the time complexity is

enter image description here