Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
31
Task/Continued-fraction/D/continued-fraction.d
Normal file
31
Task/Continued-fraction/D/continued-fraction.d
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import std.stdio, std.functional, std.traits;
|
||||
|
||||
FP calc(FP, F)(in F fun, in int n) pure nothrow if (isCallable!F) {
|
||||
FP temp = 0;
|
||||
|
||||
foreach_reverse (immutable ni; 1 .. n + 1) {
|
||||
immutable p = fun(ni);
|
||||
temp = p[1] / (FP(p[0]) + temp);
|
||||
}
|
||||
return fun(0)[0] + temp;
|
||||
}
|
||||
|
||||
int[2] fSqrt2(in int n) pure nothrow {
|
||||
return [n > 0 ? 2 : 1, 1];
|
||||
}
|
||||
|
||||
int[2] fNapier(in int n) pure nothrow {
|
||||
return [n > 0 ? n : 2, n > 1 ? (n - 1) : 1];
|
||||
}
|
||||
|
||||
int[2] fPi(in int n) pure nothrow {
|
||||
return [n > 0 ? 6 : 3, (2 * n - 1) ^^ 2];
|
||||
}
|
||||
|
||||
alias print = curry!(writefln, "%.19f");
|
||||
|
||||
void main() {
|
||||
calc!real(&fSqrt2, 200).print;
|
||||
calc!real(&fNapier, 200).print;
|
||||
calc!real(&fPi, 200).print;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue