Last active
February 24, 2020 23:21
-
-
Save dniklaus/6867b7e5220df23cca212eb6516be285 to your computer and use it in GitHub Desktop.
Square Root - iterative according to Heron
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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