tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
18
Task/Nth-root/Java/nth-root-1.java
Normal file
18
Task/Nth-root/Java/nth-root-1.java
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
public static double nthroot(int n, double A) {
|
||||
return nthroot(n, A, .001);
|
||||
}
|
||||
public static double nthroot(int n, double A, double p) {
|
||||
if(A < 0) {
|
||||
System.err.println("A < 0");// we handle only real positive numbers
|
||||
return -1;
|
||||
} else if(A == 0) {
|
||||
return 0;
|
||||
}
|
||||
double x_prev = A;
|
||||
double x = A / n; // starting "guessed" value...
|
||||
while(Math.abs(x - x_prev) > p) {
|
||||
x_prev = x;
|
||||
x = ((n - 1.0) * x + A / Math.pow(x, n - 1.0)) / n;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
15
Task/Nth-root/Java/nth-root-2.java
Normal file
15
Task/Nth-root/Java/nth-root-2.java
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
public static double nthroot(int n, double x) {
|
||||
assert (n > 1 && x > 0);
|
||||
int np = n - 1;
|
||||
double g1 = x;
|
||||
double g2 = iter(g1, np, n, x);
|
||||
while (g1 != g2) {
|
||||
g1 = iter(g1, np, n, x);
|
||||
g2 = iter(iter(g2, np, n, x), np, n, x);
|
||||
}
|
||||
return g1;
|
||||
}
|
||||
|
||||
private static double iter(double g, int np, int n, double x) {
|
||||
return (np * g + x / Math.pow(g, np)) / n;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue