Javascript Algorithm Number calculate square root
function squareRootBisection(num){ var min=1, max=num, epsilon = num/1000000, // 10^(-6); mid,error;//w w w. ja v a 2s. c om // square root of numbers smaller than 1 is bigger than the number if(num<=1.0){ min = num; max = 1.0; } mid = (min+max)/2; error = mid*mid-num; while(Math.abs(error)>epsilon){ if(error<0){ min = mid; } else { max = mid; } mid = (min+max)/2; error = mid*mid-num; } return mid; } console.log(squareRootBisection(4));
Another implementation
function squareRootNewtonRaphson(num){ var sqroot = num/2, epsilon = num/1000000; //10^(-6); var error = sqroot*sqroot - num; while(Math.abs(error)>epsilon){ // xn+1 = xn - (xn*xn - num)/(2*xn); sqroot = sqroot - (error)/(2*sqroot); error = sqroot*sqroot - num;/*from w ww . jav a 2 s . c o m*/ } return sqroot; } console.log(squareRootNewtonRaphson(4));