Friday, August 15, 2014

Sqrt(x)

Honestly speaking, I am not very familiar with  "numeric calculation" kind question, since I met that sort of problems not often. Last one I worked on is pow(x,n), which I did not find out a valid solution. Thanks to code_ganker for his code and explanation. After reading his post about pow(x,n), I learned that "divide by 2" and "bit manipulation based on 2" are 2 basic useful methods. This time, "divide by 2" is applied to sqrt(x). Actually, my method is like binary search, and the target for search is to find out value res, whose square is smaller than or equal to x, and  (res + 1)^2 is larger than x. However, I did not consider the case where square of res might overflow, thus my submission can not get accepted, until I checked out what code_ganker said. Thanks to him again. Below is his post:
http://blog.csdn.net/linhuanmars/article/details/20089131

https://zh.wikipedia.org/wiki/%E7%89%9B%E9%A1%BF%E6%B3%95

No comments:

Post a Comment