Skip to content

Instantly share code, notes, and snippets.

@berkslv
Created January 22, 2021 20:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berkslv/5fa5eaf8632c88ec1e7bc18582103035 to your computer and use it in GitHub Desktop.
Save berkslv/5fa5eaf8632c88ec1e7bc18582103035 to your computer and use it in GitHub Desktop.
Newton method for square root calculation in JavaScript. This code can be improved.
const newtonSquareRoot = (num) => {
let x = 1;
let result;
for (let i = 0; i < 20; i++)
{
result = x - ( x*x - num )/( 2*x );
x = result;
}
return result;
}
let number = 123;
console.log(newtonSquareRoot(number));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment