Bakhshali Approximation for computing square roots | javascript program gives result approximated to 5 decimal places . { ; This will be the nearest perfect square to s ; This is the sqrt of pSq ; Find the nearest perfect square to s ; calculate d ; calculate P ; calculate A ; calculate sqrt ( S ) . ; Driver program
function sqroot ( s ) { var pSq = 0 ; var N = 0 ; for ( i = parseInt ( s ) ; i > 0 ; i -- ) { for ( j = 1 ; j < i ; j ++ ) { if ( j * j == i ) { pSq = i ; N = j ; break ; } } if ( pSq > 0 ) break ; } var d = s - pSq ; var P = ( d / ( 2.0 * N ) ) ; var A = N + P ; var sqrt_of_s = A - ( ( P * P ) / ( 2.0 * A ) ) ; return sqrt_of_s ; } var num = 9.2345 ; var sqroot_of_num = sqroot ( num ) ; document . write ( " " + num + " " + ( Math . round ( sqroot_of_num * 100000.0 ) / 100000.0 ) . toFixed ( 6 ) ) ;