What's the name of this finding square root algorithm?

筅森魡賤 提交于 2019-12-23 22:55:42

问题


The algorithm is as follows:

res <- 0
for i from 15 downto 0 do:
   change the ith bit of result to 1
   if res^2 > x then:
      change the ith bit of res back to 0
return res

I completely understand how it works, but I don't know what this method is called. I've been looking at the wiki for computing methods of square root but to no avail. Is this the digit-by-digit method?

(related: How to compute the integer square root of a number in x86-64, without using div?)


回答1:


As Peter Cordes mentioned in comments, this is digit-by-digit algorithm (here binary digits).

It is kind of binary search. You set i-th bit if squared result becomes closer to x but does not exceed it, so adding needed powers of two approximates result better and better.



来源:https://stackoverflow.com/questions/46481471/whats-the-name-of-this-finding-square-root-algorithm

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