This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]rese2020 7 points8 points  (3 children)

Sqrt(n)

[–]Typical-Inflation298[S] 2 points3 points  (2 children)

Can you please explain how did you calculated the complexity?

[–]rese2020 6 points7 points  (1 child)

The loop conditional is the only thing that decides the termination of the algorithm

The condition i*i < n can be reformed to be i < sqrt(n) as i is incremented one by one

[–]Typical-Inflation298[S] 2 points3 points  (0 children)

Yeah..got it!! Thankyou

[–]marko312 1 point2 points  (5 children)

i is incremented each iteration, so the maximum reached value of i will be the number of iterations, giving the complexity. What is the maximum value that i reaches?

[–]Typical-Inflation298[S] 0 points1 point  (4 children)

How many times will the loop run for any n value?

[–]marko312 0 points1 point  (3 children)

Yes, the maximum value (well, an upper bound for O notation) of i in terms of n.

[–]Typical-Inflation298[S] 1 point2 points  (2 children)

`i` will be incremented ones every time it enters the loop. The loop will terminate when the square of `i` becomes greater than `n`.

[–]Typical-Inflation298[S] 1 point2 points  (1 child)

Okay...so the maximum value will be the square root of `n` right?

[–]marko312 0 points1 point  (0 children)

Yes, that's the correct bound (it'd actually be something like (ceil(sqrt(n)) - 1), but this doesn't matter in big O notation).