Bakhshali Approximation for computing square roots | This PHP 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 Code
< ? php function sqroot ( $ s ) { $ pSq = 0 ; $ N = 0 ; for ( $ i = intval ( $ s ) ; $ i > 0 ; $ i -- ) { for ( $ j = 1 ; $ j < $ i ; $ j ++ ) { if ( $ j * $ j == $ i ) { $ pSq = $ i ; $ N = $ j ; break ; } } if ( $ pSq > 0 ) break ; } $ d = $ s - $ pSq ; $ P = $ d / ( 2.0 * $ N ) ; $ A = $ N + $ P ; $ sqrt_of_s = $ A - ( ( $ P * $ P ) / ( 2.0 * $ A ) ) ; return $ sqrt_of_s ; } $ num = 9.2345 ; $ sqroot_of_num = sqroot ( $ num ) ; echo " Square ▁ root ▁ of ▁ " . $ num . " ▁ = ▁ " . round ( ( $ sqroot_of_num * 100000.0 ) / 100000.0 , 5 ) ; ? >