Tuesday, March 19, 2013

int sqrt(int n)

Solution: using newton method:

Solving f(x) = 0, using iterative method: x_1 = x_0 - f(x_0) / f'(x_0).


So the iterative for sqrt(n) is

y = x/2 + N/(2x)



Implementation:


x = 2^ceil(numbits(N)/2)
loop:
    y = floor((x + floor(N/x))/2)
    if y >= x
        return x
    x = y

No comments:

Post a Comment

Manacher's Longest Palindromic Substring Algorithm

http://manacher-viz.s3-website-us-east-1.amazonaws.com/#/