September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,5 @@
cf::{[f g i];f::x;g::y;i::z;
f(0)+z{i::i-1;g(i+1)%f(i+1)+x}:*0}
cf({:[0=x;1;2]};{x;1};1000)
cf({:[0=x;2;x]};{:[x>1;x-1;x]};1000)
cf({:[0=x;3;6]};{((2*x)-1)^2};1000)

View file

@ -0,0 +1,21 @@
// version 1.1.2
typealias Func = (Int) -> IntArray
fun calc(f: Func, n: Int): Double {
var temp = 0.0
for (i in n downTo 1) {
val p = f(i)
temp = p[1] / (p[0] + temp)
}
return f(0)[0] + temp
}
fun main(args: Array<String>) {
val pList = listOf<Pair<String, Func>>(
"sqrt(2)" to { n -> intArrayOf(if (n > 0) 2 else 1, 1) },
"e " to { n -> intArrayOf(if (n > 0) n else 2, if (n > 1) n - 1 else 1) },
"pi " to { n -> intArrayOf(if (n > 0) 6 else 3, (2 * n - 1) * (2 * n - 1)) }
)
for (pair in pList) println("${pair.first} = ${calc(pair.second, 200)}")
}

View file

@ -0,0 +1,4 @@
contfrac:=n->evalf(Value(NumberTheory:-ContinuedFraction(n)));
contfrac(2^(0.5));
contfrac(Pi);
contfrac(exp(1));

View file

@ -0,0 +1,23 @@
function continued_fraction(integer steps, integer rid_a, integer rid_b)
atom res = 0
for n=steps to 1 by -1 do
res := call_func(rid_b,{n}) / (call_func(rid_a,{n}) + res)
end for
return call_func(rid_a,{0}) + res
end function
function sqr2_a(integer n) return iff(n=0?1:2) end function
function sqr2_b(integer n) return 1 end function
function nap_a(integer n) return iff(n=0?2:n) end function
function nap_b(integer n) return iff(n=1?1:n-1) end function
function pi_a(integer n) return iff(n=0?3:6) end function
function pi_b(integer n) return iff(n=1?1:power(2*n-1,2)) end function
constant precision = 10000
printf(1,"Precision: %d\n", {precision})
printf(1,"Sqr(2): %.10g\n", {continued_fraction(precision, routine_id("sqr2_a"), routine_id("sqr2_b"))})
printf(1,"Napier: %.10g\n", {continued_fraction(precision, routine_id("nap_a"), routine_id("nap_b"))})
printf(1,"Pi: %.10g\n", {continued_fraction(precision, routine_id("pi_a"), routine_id("pi_b"))})

View file

@ -0,0 +1,4 @@
fcn cf(fa,fb,a0){fcn(fa,fb,a0,n){
a0 + [n..1,-1].reduce(
'wrap(p,n){ fb(n)/(fa(n)+p) },0.0) }.fp(fa,fb,a0)
}

View file

@ -0,0 +1,6 @@
sqrt2:=cf((2.0).noop,(1.0).noop,1.0);
sqrt2(200) : "%.20e".fmt(_).println();
nap:=cf((0.0).create,fcn(n){ (n==1) and 1.0 or (n-1).toFloat() },2.0);
println(nap(15) - (1.0).e);
pi:=cf((6.0).noop,fcn(n){ n=2*n-1; (n*n).toFloat() },3.0);
println(pi(1000) - (1.0).pi);