Skip to content

Instantly share code, notes, and snippets.

@dniklaus
Last active February 24, 2020 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dniklaus/6867b7e5220df23cca212eb6516be285 to your computer and use it in GitHub Desktop.
Save dniklaus/6867b7e5220df23cca212eb6516be285 to your computer and use it in GitHub Desktop.
Square Root - iterative according to Heron
#include <iostream>
using namespace std;
int main()
{
int x = 10;
double a = 1.0 * x;
double l = 1.0;
const double eps = 0.000001;
int iter = 0;
while ((a - l) > eps)
{
a = (l + a) / 2.0;
l = x / a;
iter++;
}
cout << "SQRT(" << x << ") = " << l << " - " << iter << " iterations" << endl;
cout << "Check: l^2 = " << l * l << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment