Do the two statements cost the same amount of time?# Programming - 葵花宝典
t*s
1 楼
1) for i =1 to floor(sqrt(n))
2) for i=1 to n
I guess that they take different amount of time.
The reason is that in the first one each check to see whether i exceeds
the limit involves a floor(sqrt(n)) operation, while the second one
does not.
Or maybe the limit 'floor(sqrt(n))' is calculated only once and therefore I
am wrong ?
Can somebody confirm please?
2) for i=1 to n
I guess that they take different amount of time.
The reason is that in the first one each check to see whether i exceeds
the limit involves a floor(sqrt(n)) operation, while the second one
does not.
Or maybe the limit 'floor(sqrt(n))' is calculated only once and therefore I
am wrong ?
Can somebody confirm please?