Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created January 21, 2020 00:01
Show Gist options
  • Save misterpoloy/6efe14b7c8645e6233cdc4338361eee3 to your computer and use it in GitHub Desktop.
Save misterpoloy/6efe14b7c8645e6233cdc4338361eee3 to your computer and use it in GitHub Desktop.
Get square root using Newtons formula = The equation 𝑛𝑒𝑀𝑔𝑒𝑒𝑠𝑠=12βˆ—(π‘œπ‘™π‘‘π‘”π‘’π‘’π‘ π‘ +π‘›π‘œπ‘™π‘‘π‘”π‘’π‘’π‘ π‘ )
#include <iostream>
using namespace std;
double squareroot(double n) { /*return type int which indicates
that a decimal is being returned*/
double root = n / 2;
for (int i = 0; i < 20; i++) {
root = (.5) * (root + (n / root));
}
return root;
}
int main() {
cout << squareroot(9) << endl;
cout << squareroot(4563) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment