Check Machin-like formulas: Difference between revisions
Content added Content deleted
(Updated D entry) |
|||
Line 34: | Line 34: | ||
This uses the module of the Arithmetic Rational Task. |
This uses the module of the Arithmetic Rational Task. |
||
{{trans|Python}} |
{{trans|Python}} |
||
<lang d>import std.stdio, std.regex, std.conv, std.string, std. |
<lang d>import std.stdio, std.regex, std.conv, std.string, std.range, |
||
arithmetic_rational; |
|||
import arithmetic_rational: Rational; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
auto r = regex(r"\s*(?P<sign>[+-])?\s*(?:(?P<mul>\d+)\s*\*)?\s*" ~ |
auto r = regex(r"\s*(?P<sign>[+-])?\s*(?:(?P<mul>\d+)\s*\*)?\s*" ~ |
||
r"arctan\((?P<num>\d+)/(?P<denom>\d+)\) |
r"arctan\((?P<num>\d+)/(?P<denom>\d+)\)"); |
||
Pair[][] machins; |
Pair[][] machins; |
||
foreach (const line; text.splitLines |
foreach (const line; text.splitLines) { |
||
Pair[] formula; |
Pair[] formula; |
||
foreach (part; |
foreach (/*const*/ part; line.split("=")[1].matchAll(r)) { |
||
immutable |
immutable mul = part["mul"], |
||
num = part["num"], |
|||
denom = part["denom"]; |
|||
formula ~= Pair((part["sign"] == "-" ? -1 : 1) * |
formula ~= Pair((part["sign"] == "-" ? -1 : 1) * |
||
(mul.empty ? 1 : to!int |
(mul.empty ? 1 : mul.to!int), |
||
Rational(to!int |
Rational(num.to!int, |
||
denom.empty ? 1 : to!int |
denom.empty ? 1 : denom.to!int)); |
||
} |
} |
||
machins ~= formula; |
machins ~= formula; |
||
Line 87: | Line 67: | ||
if (coef < 0) |
if (coef < 0) |
||
return -tanEval(-coef, f); |
return -tanEval(-coef, f); |
||
/*const*/ auto a = tanEval(coef / 2, f) |
/*const*/ auto a = tanEval(coef / 2, f), |
||
b = tanEval(coef - coef / 2, f); |
|||
return (a + b) / (1 - a * b); |
return (a + b) / (1 - a * b); |
||
} |
} |
||
Line 94: | Line 74: | ||
if (xs.length == 1) |
if (xs.length == 1) |
||
return tanEval(xs[0].tupleof); |
return tanEval(xs[0].tupleof); |
||
/*const*/ auto a = |
/*const*/ auto a = xs[0 .. $ / 2].tans, |
||
b = xs[$ / 2 .. $].tans; |
|||
return (a + b) / (1 - a * b); |
return (a + b) / (1 - a * b); |
||
} |
} |
||
void main() { |
void main() { |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
writefln("%5s: %s", ans == 1 ? "OK" : "ERROR", eqn); |
writefln("%5s: %s", ans == 1 ? "OK" : "ERROR", eqn); |
||
} |
} |