
import math

def squareRoot (num, precise = 3):
    ''' Returns closest square-root of a number,
        using Bisection search algorithm.
        @num : The number for which square root is needed
        @precise: Precise upto number precise number of digits'''
    if num < 0:
        return -1
    elif num == 0:
        return 0
    step = 0.1
    precission = step / ((10**precise))
    start = 0
    end = num
    while ( True ):
        mid = (start + end) / 2.0
        squareTillNow = mid*mid
        if abs(squareTillNow - num) < precission:
            break
        else:
            if squareTillNow > num:
                end = mid
            else :
                start = mid
    return mid
     
print squareRoot (10000, 4)

@@ -21,16 +21,22 @@
 uareRoot
+Bisect
  (num, p
@@ -259,16 +259,21 @@
 f digits
+%0A    
 '''%0A    
@@ -706,49 +706,967 @@
     
-return mid%0A     %0Aprint squareRoot (10
+    print %22Mid : %22, mid%0A    return mid%0A     %0A%0Adef squareRootNR (num, precise = 3):%0A    ''' Returns closest square-root of a number,%0A        using Newton-Rapson approximation algorithm.%0A        @num : The number for which square root is needed%0A        @precise: Precise upto number precise number of digits%0A    '''%0A    if num %3C 0:%0A        return -1%0A    elif num == 0:%0A        return 0%0A    step = 0.1%0A    precission = step / ((10**precise))%0A    guess = num / 2.0%0A    while (True):%0A        squareTillNow = guess * guess%0A        if abs(squareTillNow - num) %3C precission:%0A            break%0A        else :%0A            guess = guess - ((squareTillNow - num) / (2*guess))%0A            ''' x(n+1) = f( x(n) ) - ( f( x(n) ) / f'( x(n) ) ) %0A                Where f(x (n)) is (x(n) - num )%5E2 %0A                f (x) is approximate squared error %0A            '''%0A        print %22Guess : %22, guess%0A    return guess%0A%0A%0Aprint squareRootBisect (1
 000
+1
 , 4)%0A
+print squareRootNR (10001, 6)%0A

Newton-Rapson approximation