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,29 @@
func continued_fraction(a, _, (0)) { a() }
func continued_fraction(a, b, n=100) {
a() + (b() / continued_fraction(a, b, n-1));
}
var f = Hash.new(
"√2" => [
do { var n = 0; { n++ ? 2 : 1 } },
{ 1 },
],
"e" => [
do { var n = 0; { n++ || 2 } },
do { var n = 0; { n++ || 1 } },
],
"π" => [
do { var n = 0; { n++ ? 6 : 3 } },
do { var n = 0; { (2*(n++) + 1)**2 } },
1_000,
],
"π/2" => [
do { var n = 0; { 1/(n++ || 1) } },
{ 1 },
1_000,
]
);
f.each { |k,v|
printf("%3s ≈ %.9f\n", k, continued_fraction(v...));
}

View file

@ -0,0 +1,25 @@
# "first" is the first triple,
# e.g. [1,a,b]; count specifies the number of terms to use.
def continued_fraction( first; next; count ):
# input: [i, a, b]]
def cf:
if .[0] == count then 0
else next as $ab
| .[1] + (.[2] / ($ab | cf))
end ;
first | cf;
# "first" and "next" are as above;
# if delta is 0 then continue until there is no detectable change.
def continued_fraction_delta(first; next; delta):
def abs: if . < 0 then -. else . end;
def cf:
# state: [n, prev]
.[0] as $n | .[1] as $prev
| continued_fraction(first; next; $n+1) as $this
| if $prev == null then [$n+1, $this] | cf
elif delta <= 0 and ($prev == $this) then $this
elif (($prev - $this)|abs) <= delta then $this
else [$n+1, $this] | cf
end;
[2,null] | cf;

View file

@ -0,0 +1,4 @@
"Value : Direct : Continued Fraction",
"2|sqrt : \(2|sqrt) : \(continued_fraction_delta( [1,1,1]; [.[0]+1, 2, 1]; 0))",
"1|exp : \(1|exp) : \(2 + (1 / (continued_fraction_delta( [1,1,1]; [.[0]+1, .[1]+1, .[2]+1]; 0))))",
"pi : \(1|atan * 4) : \(continued_fraction_delta( [1,3,1]; .[0]+1 | [., 6, ((2*. - 1) | (.*.))]; 1e-12)) (1e-12)"

View file

@ -0,0 +1,5 @@
$ jq -M -n -r -f Continued_fraction.jq
Value : Direct : Continued Fraction
2|sqrt : 1.4142135623730951 : 1.4142135623730951
1|exp : 2.718281828459045 : 2.7182818284590455
pi : 3.141592653589793 : 3.1415926535892935 (1e-12)