Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
29
Task/Continued-fraction/Sidef/continued-fraction.sidef
Normal file
29
Task/Continued-fraction/Sidef/continued-fraction.sidef
Normal 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...));
|
||||
}
|
||||
25
Task/Continued-fraction/jq/continued-fraction-1.jq
Normal file
25
Task/Continued-fraction/jq/continued-fraction-1.jq
Normal 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;
|
||||
4
Task/Continued-fraction/jq/continued-fraction-2.jq
Normal file
4
Task/Continued-fraction/jq/continued-fraction-2.jq
Normal 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)"
|
||||
5
Task/Continued-fraction/jq/continued-fraction-3.jq
Normal file
5
Task/Continued-fraction/jq/continued-fraction-3.jq
Normal 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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue