blob: aa6b0320c9378fefeca13ffd3f5b8031a4f4ac8f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
#include<iostream>
using namespace std;
double abs(double n) {
if(n < 0) return -n;
return n;
}
double sq(int n) {
double x = n;
double y = 1;
while(x-y > 0.0000001) {
x = (x+y)/2;
y = n/x;
}
return x;
}
double sq2_n(double n, double a) {
if(abs(a*a - n) <= 0.000001) {
return a;
}
return sq2_n(n, (a+n/a)/2);
}
double sq2(int n) {
return sq2_n(n, n/2);
}
double sqrt_d(int n) {
double x = 1;
while(abs(x*x-n)>=0.0000001) {
x = ((n/x)+x)/2;
}
return x;
}
int main() {
cout << sq(81) << endl;
cout << sq2(81) << endl;
return 0;
}
|