Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,22 @@
include "ConsoleWindow"
def tab 8
local fn NthRoot( root as long, a as long, precision as double ) as double
dim as double x0, x1
x0 = a : x1 = a /root
while ( abs( x1 - x0 ) > precision )
x0 = x1
x1 = ( ( root -1.0 ) * x1 + a / x1 ^ ( root -1.0 ) ) /root
wend
end fn = x1
print " 125th Root of 5643 Precision .001", using "#.###############"; fn NthRoot( 125, 5642, 0.001 )
print " 125th Root of 5643 Precision .001", using "#.###############"; fn NthRoot( 125, 5642, 0.001 )
print " 125th Root of 5643 Precision .00001", using "#.###############"; fn NthRoot( 125, 5642, 0.00001 )
print " Cube Root of 27 Precision .00001", using "#.###############"; fn NthRoot( 3, 27, 0.00001 )
print "Square Root of 2 Precision .00001", using "#.###############"; fn NthRoot( 2, 2, 0.00001 )
print "Square Root of 2 Precision .00001", using "#.###############"; sqr(2) // Processor floating point calc deviation
print " 10th Root of 1024 Precision .00001", using "#.###############"; fn NthRoot( 10, 1024, 0.00001 )
print " 5th Root of 34 Precision .00001", using "#.###############"; fn NthRoot( 5, 34, 0.00001 )

View file

@ -0,0 +1,3 @@
on nthRoot (x, root)
return power(x, 1.0/root)
end

View file

@ -0,0 +1,3 @@
the floatPrecision = 8 -- only about display/string cast of floats
put nthRoot(4, 4)
-- 1.41421356

View file

@ -0,0 +1,13 @@
import math
proc nthroot(a, n): float =
var n = float(n)
result = a
var x = a / n
while abs(result-x) > 10e-15:
x = result
result = (1.0/n) * (((n-1)*x) + (a / pow(x, n-1)))
echo nthroot(34.0, 5)
echo nthroot(42.0, 10)
echo nthroot(5.0, 2)

View file

@ -0,0 +1,2 @@
Float method: nthroot(n)
1.0 doWhile: [ self over n 1 - pow / over - n / tuck + swap 0.0 <> ] ;

View file

@ -0,0 +1,12 @@
decimals(12)
see "cube root of 5 is : " + root(3, 5, 0) + nl
func root n, a, d
y = 0 x = a / n
while fabs (x - y) > d
y = ((n - 1)*x + a/pow(x,(n-1))) / n
temp = x
x = y
y = temp
end
return x

View file

@ -0,0 +1,11 @@
func nthroot(n, a, precision=1e-5) {
var x = 1;
var prev = 0;
while ((prev-x).abs > precision) {
prev = x;
x = (((n-1)*prev + a/(prev**(n-1))) / n);
};
return x;
}
say nthroot(5, 34); # => 2.024397458501034082599817835297912829678

View file

@ -0,0 +1,6 @@
func nthroot_fast(n, a, precision=1e-5) {
{ a = nthroot(2, a, precision) } * int(n-1);
a ** (2**int(n-1) / n);
}
say nthroot_fast(5, 34, 1e-64); # => 2.024397458499885042510817245541937419115

View file

@ -0,0 +1,31 @@
# An iterative algorithm for finding: self ^ (1/n) to the given
# absolute precision if "precision" > 0, or to within the precision
# allowed by IEEE 754 64-bit numbers.
# The following implementation handles underflow caused by poor estimates.
def iterative_nth_root(n; precision):
def abs: if . < 0 then -. else . end;
def sq: .*.;
def pow(p): . as $in | reduce range(0;p) as $i (1; . * $in);
def _iterate: # state: [A, x1, x2, prevdelta]
.[0] as $A | .[1] as $x1 | .[2] as $x2 | .[3] as $prevdelta
| ( $x2 | pow(n-1)) as $power
| if $power <= 2.155094094640383e-309
then [$A, $x1, ($x1 + $x2)/2, n] | _iterate
else (((n-1)*$x2 + ($A/$power))/n) as $x1
| (($x1 - $x2)|abs) as $delta
| if (precision == 0 and $delta == $prevdelta and $delta < 1e-15)
or (precision > 0 and $delta <= precision) or $delta == 0 then $x1
else [$A, $x2, $x1, $delta] | _iterate
end
end
;
if n == 1 then .
elif . == 0 then 0
elif . < 0 then error("iterative_nth_root: input \(.) < 0")
elif n != (n|floor) then error("iterative_nth_root: argument \(n) is not an integer")
elif n == 0 then error("iterative_nth_root(0): domain error")
elif n < 0 then 1/iterative_nth_root(-n; precision)
else [., ., (./n), n, 0] | _iterate
end
;

View file

@ -0,0 +1,10 @@
def demo(x):
def nth_root(n): log / n | exp;
def lpad(n): tostring | (n - length) * " " + .;
. as $in
| "\(x)^(1/\(lpad(5))): \(x|nth_root($in)|lpad(18)) vs \(x|iterative_nth_root($in; 1e-10)|lpad(18)) vs \(x|iterative_nth_root($in; 0))"
;
# 5^m for various values of n:
"5^(1/ n): builtin precision=1e-10 precision=0",
( (1,-5,-3,-1,1,3,5,1000,10000) | demo(5))

View file

@ -0,0 +1,11 @@
$ jq -n -r -f nth_root_machine_precision.jq
5^(1/ n): builtin precision=1e-10 precision=0
5^(1/ 1): 4.999999999999999 vs 5 vs 5
5^(1/ -5): 0.7247796636776955 vs 0.7247796636776956 vs 0.7247796636776955
5^(1/ -3): 0.5848035476425733 vs 0.5848035476425731 vs 0.5848035476425731
5^(1/ -1): 0.2 vs 0.2 vs 0.2
5^(1/ 1): 4.999999999999999 vs 5 vs 5
5^(1/ 3): 1.709975946676697 vs 1.709975946676697 vs 1.709975946676697
5^(1/ 5): 1.3797296614612147 vs 1.3797296614612147 vs 1.379729661461215
5^(1/ 1000): 1.0016107337527294 vs 1.0016107337527294 vs 1.0016107337527294
5^(1/10000): 1.0001609567433902 vs 1.0001609567433902 vs 1.0001609567433902