#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
void sqrt_heron(double x, int m)
{
double r = 1.0;
for (int i = 1; i <= m; ++i) {
r = (r + x/r) / 2;
cout << i << ". Schritt sqrt(" << x << ") ~ " << setprecision(16) << r << endl;
}
}
void sqrt_is(double x, int m)
{
double a = 0.0, b = x, c;
for (int i = 1; i <= m; ++i) {
c = (a + b) / 2;
cout << i << ". Schritt sqrt(" << x << ") ~ " << setprecision(16) << c << endl;
if (c * c > x)
b = c;
else
a = c;
}
}
void sqrt_schr(double x, int m)
{
double y = x, r;
int a = 0, b = 0;
unsigned long u = 0;
while (y >= 100.0) {
y /= 100.0;
++a;
}
for (int i = 1; i <= m; ++i) {
for (b = 1; b < 10; ++b)
if (20 * u * b + b * b > y)
break;
--b;
r *= 10; r += b;
y -= 20 * u * b + b * b; y *= 100;
u *= 10; u += b;
cout << i << ". Schritt sqrt(" << x << ") ~ " << setprecision(16) << r * pow(10, a) << endl;
--a;
}
}
int main()
{
for (;;) {
int v, i;
double w;
cout << "\nVerfahren der Wurzelbestimmung:" << endl;
cout << "1 = Heron" << endl;
cout << "2 = Intervallschachtelung" << endl;
cout << "3 = schriftliches Wurzelziehen" << endl;
cout << "0 = Beenden" << endl;
cin >> v;
if (v < 1 || v > 3)
break;
cout << "Iterationsschritte: " << flush;
cin >> i;
if (i < 1)
break;
cout << "Radikand: " << flush;
cin >> w;
if (w <=0)
break;
cout << "Die Quadratwurzel aus " << w << " ist " << setprecision(16) << sqrt(w) << " (zum Vergleich)." << endl;
switch (v) {
case 1: cout << "Heron:" << endl;
sqrt_heron(w, i);
break;
case 2: cout << "Intervallschachtelung:" << endl;
sqrt_is(w, i);
break;
case 3: cout << "schriftliches Wurzelziehen:" << endl;
sqrt_schr(w, i);
break;
};
}
return 0;
}