c++ - What's the complexity of this algorithm -


this question has answer here:

what time complexity of algorithm?

void prime(int n) {      int = 2;     while ((n % i) && <= sqrt(n))         i++;      if (i > sqrt(n))         print(“%d prime number\n”, n);     else         print(“%d not prime number\n”, n); } 

the complexity approximately o(sqrt(n)). books express o(n0.5).

the square root re-computed each iteration of loop. slow operation, it's slower optimal, constant factor, doesn't affect computational complexity.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -