Check Machin-like formulas: Difference between revisions

Add Seed7 example
(+ D entry)
(Add Seed7 example)
Line 715:
 
'''Note:''' the [http://kodos.sourceforge.net/ Kodos] tool was used in developing the regular expression.
 
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
include "bigint.s7i";
include "bigrat.s7i";
 
const type: mTerms is array array bigInteger;
 
const array mTerms: testCases is [] (
[] ([] ( 1_, 1_, 2_), [] ( 1_, 1_, 3_)),
[] ([] ( 2_, 1_, 3_), [] ( 1_, 1_, 7_)),
[] ([] ( 4_, 1_, 5_), [] (-1_, 1_, 239_)),
[] ([] ( 5_, 1_, 7_), [] ( 2_, 3_, 79_)),
[] ([] ( 1_, 1_, 2_), [] ( 1_, 1_, 5_), [] ( 1_, 1_, 8_)),
[] ([] ( 4_, 1_, 5_), [] (-1_, 1_, 70_), [] ( 1_, 1_, 99_)),
[] ([] ( 5_, 1_, 7_), [] ( 4_, 1_, 53_), [] ( 2_, 1_, 4443_)),
[] ([] ( 6_, 1_, 8_), [] ( 2_, 1_, 57_), [] ( 1_, 1_, 239_)),
[] ([] ( 8_, 1_, 10_), [] (-1_, 1_, 239_), [] ( -4_, 1_, 515_)),
[] ([] (12_, 1_, 18_), [] ( 8_, 1_, 57_), [] ( -5_, 1_, 239_)),
[] ([] (16_, 1_, 21_), [] ( 3_, 1_, 239_), [] ( 4_, 3_, 1042_)),
[] ([] (22_, 1_, 28_), [] ( 2_, 1_, 443_), [] ( -5_, 1_, 1393_), [] (-10_, 1_, 11018_)),
[] ([] (22_, 1_, 38_), [] (17_, 7_, 601_), [] ( 10_, 7_, 8149_)),
[] ([] (44_, 1_, 57_), [] ( 7_, 1_, 239_), [] (-12_, 1_, 682_), [] ( 24_, 1_, 12943_)),
[] ([] (88_, 1_, 172_), [] (51_, 1_, 239_), [] ( 32_, 1_, 682_), [] ( 44_, 1_, 5357_), [] (68_, 1_, 12943_)),
[] ([] (88_, 1_, 172_), [] (51_, 1_, 239_), [] ( 32_, 1_, 682_), [] ( 44_, 1_, 5357_), [] (68_, 1_, 12944_))
);
 
const func bigRational: tanEval (in bigInteger: coef, in bigRational: f) is func
result
var bigRational: tanEval is bigRational.value;
local
var bigRational: a is bigRational.value;
var bigRational: b is bigRational.value;
begin
if coef = 1_ then
tanEval := f;
elsif coef < 0_ then
tanEval := -tanEval(-coef, f);
else
a := tanEval(coef div 2_, f);
b := tanEval(coef - coef div 2_, f);
tanEval := (a + b) / (1_/1_ - a * b);
end if;
end func;
 
const func bigRational: tans (in mTerms: terms) is func
result
var bigRational: tans is bigRational.value;
local
var bigRational: a is bigRational.value;
var bigRational: b is bigRational.value;
begin
if length(terms) = 1 then
tans := tanEval(terms[1][1], terms[1][2] / terms[1][3]);
else
a := tans(terms[.. length(terms) div 2]);
b := tans(terms[succ(length(terms) div 2) ..]);
tans := (a + b) / (1_/1_ - a * b);
end if;
end func;
 
const proc: main is func
local
var integer: index is 0;
var array bigInteger: term is 0 times 0_;
begin
for key index range testCases do
write(tans(testCases[index]) = 1_/1_ <& ": pi/4 = ");
for term range testCases[index] do
write([0] ("+", "-")[ord(term[1] < 0_)] <& abs(term[1]) <& "*arctan(" <& term[2] <& "/" <& term[3] <& ")");
end for;
writeln;
end for;
end func;</lang>
 
{{out}}
<pre>
TRUE: pi/4 = +1*arctan(1/2)+1*arctan(1/3)
TRUE: pi/4 = +2*arctan(1/3)+1*arctan(1/7)
TRUE: pi/4 = +4*arctan(1/5)-1*arctan(1/239)
TRUE: pi/4 = +5*arctan(1/7)+2*arctan(3/79)
TRUE: pi/4 = +1*arctan(1/2)+1*arctan(1/5)+1*arctan(1/8)
TRUE: pi/4 = +4*arctan(1/5)-1*arctan(1/70)+1*arctan(1/99)
TRUE: pi/4 = +5*arctan(1/7)+4*arctan(1/53)+2*arctan(1/4443)
TRUE: pi/4 = +6*arctan(1/8)+2*arctan(1/57)+1*arctan(1/239)
TRUE: pi/4 = +8*arctan(1/10)-1*arctan(1/239)-4*arctan(1/515)
TRUE: pi/4 = +12*arctan(1/18)+8*arctan(1/57)-5*arctan(1/239)
TRUE: pi/4 = +16*arctan(1/21)+3*arctan(1/239)+4*arctan(3/1042)
TRUE: pi/4 = +22*arctan(1/28)+2*arctan(1/443)-5*arctan(1/1393)-10*arctan(1/11018)
TRUE: pi/4 = +22*arctan(1/38)+17*arctan(7/601)+10*arctan(7/8149)
TRUE: pi/4 = +44*arctan(1/57)+7*arctan(1/239)-12*arctan(1/682)+24*arctan(1/12943)
TRUE: pi/4 = +88*arctan(1/172)+51*arctan(1/239)+32*arctan(1/682)+44*arctan(1/5357)+68*arctan(1/12943)
FALSE: pi/4 = +88*arctan(1/172)+51*arctan(1/239)+32*arctan(1/682)+44*arctan(1/5357)+68*arctan(1/12944)
</pre>
 
=={{header|Tcl}}==