-
Notifications
You must be signed in to change notification settings - Fork 313
/
Nth_root.c
58 lines (49 loc) · 1.03 KB
/
Nth_root.c
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
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
double pow(double x, int n) {
double xn = 1;
for(int i=0;i<n;i++) {
xn *= x;
}
return xn;
}
// x ^ n - k
double fx(double x, int n, double k) {
return pow(x,n) - k;
}
// n* x ^ n-1
double _fx(double x, int n) {
return n*pow(x,n-1);
}
// next = curr - fx() / _fx()
double next(double curr, int n, double k) {
return curr - fx(curr, n, k) / _fx(curr, n);
}
// apprx root for starting
double apprx(int n, double k) {
double gt = 0, p = 0;
while(gt < k) {
gt = pow(++p, n);
}
return p - 0.5;
}
double abs(double diff) {
return diff>0?diff:-diff;
}
int precision(double k, double _x, int n) {
return abs(k-pow(_x,n)) < 0.0001;
}
double nThRoot(double k, int n) {
double _x = apprx(n, k);
while(!precision(k, _x, n)) {
_x = next(_x, n, k);
}
return _x;
}
int main() {
int n = 2;
double k = 50;
double x = nThRoot(k,n);
printf("%d root of %lf : %lf\n",n,k,x);
printf("%lf ^ %d = %lf", x,n, pow(x,n));
return 0;
}