Time complexity of Math.sqrt Java

怎甘沉沦 提交于 2019-12-24 14:13:22

问题


What is time-complexity of math.sqrt implementation in Java ? Java has time-complexity implemented in some technique whose, time-complexity I am trying to determine.


回答1:


In most cases, Java attempts to use the "smart-power" algorithm, which results in a time-complexity of O(log n). Smart power Algorithm

Also, it appears that in different cases, you could end up with different complexities; Why is multiplied many times faster than taking the square root?




回答2:


It looks like it is implemented by delegating to the sqrt method StrictMath which is a native method.

Thus it seems the answer would be implementation specific.

Strictly speaking it is O(1). In theory (but obviously not practice), we could iterate over all doubles and find the maximum time.

In addition, the time complexity of Math.sqrt(n) does not directly depend on n but instead on the amount of space needed to represent n which for doubles should be constant.



来源:https://stackoverflow.com/questions/28815339/time-complexity-of-math-sqrt-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!