is loop --
for(int = 0 ; < sqrt(number) ; i++) { //some operations } faster --
int length = sqrt(number); for(int = 0 ; < length ; i++) { //some operations } i getting tle in code in online judge when replaced sqrt in loop length got accepted.
can u please point out time complexity of loop sqrt considering number <=1000000000
the time complexity isn't different between 2 loops (unless complexity of sqrt dependent on number) is different how many times you're computing square root.
without optimisation compiler automatically moving loop invariant stuff outside of loop (assuming that's allowed in case since compiler have check lot of things ensure can't affect result or side effects of sqrt call), following code calculate square root thousand times (once per iteration):
number = 1000000; for(int = 0 ; < sqrt(number) ; i++) { ... } however, this code calculate once:
number = 1000000; root = sqrt(number); for(int = 0 ; < root ; i++) { ... }
Comments
Post a Comment