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
Post a Comment