2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1 +1,3 @@
Implement the algorithm to compute the principal [[wp:Nth root|''n''th root]] <math>\sqrt[n]A</math> of a positive real number ''A'', as explained at the [[wp:Nth root algorithm|Wikipedia page]].
;Task:
Implement the algorithm to compute the principal [[wp:Nth root|''n''th root]] <big><math>\sqrt[n]A</math></big> of a positive real number <big>''A''</big>, as explained at the [[wp:Nth root algorithm|Wikipedia page]].

View file

@ -1,2 +1,4 @@
---
category:
- Simple
note: Classic CS problems and programs

View file

@ -1,31 +1,34 @@
#include <stdio.h>
#include <float.h>
inline double abs_(double x) { return x >= 0 ? x : -x; }
double pow_(double x, int e)
{
double ret = 1;
for (ret = 1; e; x *= x, e >>= 1)
if ((e & 1)) ret *= x;
return ret;
double pow_ (double x, int e) {
int i;
double r = 1;
for (i = 0; i < e; i++) {
r *= x;
}
return r;
}
double root(double a, int n)
{
double d, x = 1;
if (!a) return 0;
if (n < 1 || (a < 0 && !(n&1))) return 0./0.; /* NaN */
do { d = (a / pow_(x, n - 1) - x) / n;
x+= d;
} while (abs_(d) >= abs_(x) * (DBL_EPSILON * 10));
return x;
double root (int n, double x) {
double d, r = 1;
if (!x) {
return 0;
}
if (n < 1 || (x < 0 && !(n&1))) {
return 0.0 / 0.0; /* NaN */
}
do {
d = (x / pow_(r, n - 1) - r) / n;
r += d;
}
while (d >= DBL_EPSILON * 10 || d <= -DBL_EPSILON * 10);
return r;
}
int main()
{
double x = pow_(-3.14159, 15);
printf("root(%g, 15) = %g\n", x, root(x, 15));
return 0;
int main () {
int n = 15;
double x = pow_(-3.14159, 15);
printf("root(%d, %g) = %g\n", n, x, root(n, x));
return 0;
}

View file

@ -0,0 +1,23 @@
(ns test-project-intellij.core
(:gen-class))
;; define abs & power to avoid needing to bring in the clojure Math library
(defn abs [x]
" Absolute value"
(if (< x 0) (- x) x))
(defn power [x n]
" x to power n, where n = 0, 1, 2, ... "
(apply * (repeat n x)))
(defn calc-delta [A x n]
" nth rooth algorithm delta calculation "
(/ (- (/ A (power x (- n 1))) x) n))
(defn nth-root
" nth root of algorithm: A = numer, n = root"
([A n] (nth-root A n 0.5 1.0)) ; Takes only two arguments A, n and calls version which takes A, n, guess-prev, guess-current
([A n guess-prev guess-current] ; version take takes in four arguments (A, n, guess-prev, guess-current)
(if (< (abs (- guess-prev guess-current)) 1e-6)
guess-current
(recur A n guess-current (+ guess-current (calc-delta A guess-current n)))))) ; iterate answer using tail recursion

View file

@ -1,39 +1,38 @@
/*REXX program calculates the Nth root of X, with DIGS accuracy. */
parse arg x root digs . /*get specified args from the CL.*/
if x=='' then x=2 /*Not specified? Then use default*/
if root=='' then root=2 /* " " " " " */
if digs=='' then digs=65 /* " " " " " */
numeric digits digs /*set the precision to DIGS. */
say ' x = ' x /*echo the value of X. */
say ' root = ' root /*echo the value of ROOT. */
say ' digits = ' digs /*echo the value of DIGS. */
say ' answer = ' root(x,root) /*show the value of ANSWER. */
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────ROOT subroutine─────────────────────*/
root: procedure; parse arg x 1 Ox,r . 1 Or /*1st arg──►x&Ox, 2nd──►r&Or*/
if r=='' then r=2 /*Was root specified? Assume √.*/
if r=0 then return '[n/a]' /*oops-ay! Can't do zeroth root.*/
complex= x<0 & R//2==0 /*will the result be complex? */
oDigs=digits() /*get the current number of digs.*/
if x=0 | r=1 then return x/1 /*handle couple of special cases.*/
dm=oDigs+5 /*we need a little guard room. */
r=abs(r); x=abs(x) /*the absolute values of R and X.*/
rm=r-1 /*just a fast version of ROOT -1*/
numeric form /*take a good guess at the root─┐*/
parse value format(x,2,1,,0) 'E0' with ? 'E' _ . /* ◄───────────┘*/
g= (? / r'E'_ % r) + (x>1) /*kinda uses a crude "logarithm".*/
numeric fuzz 3 /*fuzz digits for higher roots. */
d=5 /*start with only five digits. */
do until d==dm; d=min(d+d,dm) /*each interation doubles prec. */
numeric digits d /*tell REXX to use D digits. */
old=-1 /*assume some kind of old guess. */
do until old=g; old=g /*where da rubber meets da road─┐*/
g=(rm*g**r+x) / r / g**rm /*nitty-gritty root computation◄┘*/
end /*until old=g*/ /*maybe until the cows come home.*/
end /*until d==dm*/ /*and wait for more cows to come.*/
/*REXX program calculates the Nth root of X, with DIGS (decimal digits) accuracy. */
parse arg x root digs . /*obtain optional arguments from the CL*/
if x=='' | x=="," then x= 2 /*Not specified? Then use the default.*/
if root=='' | root=="," then root= 2 /* " " " " " " */
if digs=='' | digs=="," then digs=65 /* " " " " " " */
numeric digits digs /*set the decimal digits to DIGS. */
say ' x = ' x /*echo the value of X. */
say ' root = ' root /* " " " " ROOT. */
say ' digits = ' digs /* " " " " DIGS. */
say ' answer = ' root(x, root) /*show the value of ANSWER. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
root: procedure; parse arg x 1 Ox, r 1 Or /*arg1 ──► x & Ox, 2nd ──► r & Or*/
if r=='' then r=2 /*Was root specified? Assume √. */
if r=0 then return '[n/a]' /*oops-ay! Can't do zeroth root.*/
complex= x<0 & R//2==0 /*will the result be complex? */
oDigs=digits() /*get the current number of digs.*/
if x=0 | r=1 then return x/1 /*handle couple of special cases.*/
dm=oDigs+5 /*we need a little guard room. */
r=abs(r); x=abs(x) /*the absolute values of R and X.*/
rm=r-1 /*just a fast version of ROOT -1*/
numeric form /*take a good guess at the root─┐*/
parse value format(x,2,1,,0) 'E0' with ? 'E' _ . /* ◄────────────────────────────┘*/
g= (? / r'E'_ % r) + (x>1) /*kinda uses a crude "logarithm".*/
d=5 /*start with five decimal digits.*/
do until d==dm; d=min(d+d,dm) /*each time, precision doubles. */
numeric digits d /*tell REXX to use D digits. */
old=-1 /*assume some kind of old guess. */
do until old=g; old=g /*where da rubber meets da road─┐*/
g=format((rm*g**r+x)/r/g**rm,, d-2) /* ◄────── the root computation─┘*/
end /*until old=g*/ /*maybe until the cows come home.*/
end /*until d==dm*/ /*and wait for more cows to come.*/
if g=0 then return 0 /*in case the jillionth root = 0.*/
if Or<0 then g=1/g /*root < 0 ? Reciprocal it is!*/
if \complex then g=g*sign(Ox) /*adjust the sign (maybe). */
numeric digits oDigs /*reinstate the original digits. */
return g/1 || left('j',complex) /*normalize # to digs, append j ?*/
if g=0 then return 0 /*in case the jillionth root = 0.*/
if Or<0 then g=1/g /*root < 0 ? Reciprocal it is! */
if \complex then g=g*sign(Ox) /*adjust the sign (maybe). */
numeric digits oDigs /*reinstate the original digits. */
return (g/1) || left('j', complex) /*normalize # to digs, append j ?*/