From f279107065146a4940f5e73602a1c3c09e58b31d Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sun, 18 Oct 2020 18:56:43 +0200 Subject: chore: name of first year folder --- 1_anno/Programmazione_2/algorithms/sqrt.cc | 46 ++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 1_anno/Programmazione_2/algorithms/sqrt.cc (limited to '1_anno/Programmazione_2/algorithms/sqrt.cc') diff --git a/1_anno/Programmazione_2/algorithms/sqrt.cc b/1_anno/Programmazione_2/algorithms/sqrt.cc new file mode 100644 index 0000000..aa6b032 --- /dev/null +++ b/1_anno/Programmazione_2/algorithms/sqrt.cc @@ -0,0 +1,46 @@ +#include + +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; +} -- cgit v1.2.3-18-g5258