Check Machin-like formulas: Difference between revisions

m (Automated syntax highlighting fixup (second round - minor fixes))
Line 2,392:
bad: pi/4 = 88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 67.9999999994*atan(1/12943)
</pre>
=={{header|RPL}}==
RPL does not support fat integers, therefore fat fractions neither, but the precision of floating-point numbers is actually sufficient to detect the incorrect formula and validate the other ones without using any trigonometric function. THe 17 formulas to check are stored as strings in a global variable:
{ "1*arctan(1/2) + 1*arctan(1/3)"
"2*arctan(1/3) + 1*arctan(1/7)"
"4*arctan(1/5) + -1*arctan(1/239)"
"5*arctan(1/7) + 2*arctan(3/79)"
"5*arctan(29/278) + 7*arctan(3/79)"
"1*arctan(1/2) + 1*arctan(1/5) + 1*arctan(1/8)"
"4*arctan(1/5) + -1*arctan(1/70) + 1*arctan(1/99)"
"5*arctan(1/7) + 4*arctan(1/53) + 2*arctan(1/4443)"
"6*arctan(1/8) + 2*arctan(1/57) + 1*arctan(1/239)"
"8*arctan(1/10) + -1*arctan(1/239) + -4*arctan(1/515)"
"12*arctan(1/18) + 8*arctan(1/57) + -5*arctan(1/239)"
"16*arctan(1/21) + 3*arctan(1/239) + 4*arctan(3/1042)"
"22*arctan(1/28) + 2*arctan(1/443) + -5*arctan(1/1393) + -10*arctan(1/11018)"
"22*arctan(1/38) + 17*arctan(7/601) + 10*arctan(7/8149)"
"44*arctan(1/57) + 7*arctan(1/239) + -12*arctan(1/682) + 24*arctan(1/12943)"
"88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12943)"
"88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12944)"
} ''''Formulas'''' STO
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
IF DUP 1 == THEN DROP
ELSE IF DUP 0 < THEN NEG '''TanEval''' NEG
ELSE DUP2 2 / IP '''TanEval''' ROT ROT
DUP 2 / IP - '''TanEval'''
+ LAST * 1 - NEG /
END END
≫ ‘'''TanEval'''’ STO
DUP 2 GET →NUM OVER 1 GET →NUM '''TanEval''' SWAP
IF DUP SIZE 3 ≥ THEN
LIST→ 2 - →LIST ROT ROT DROP2
'''TanSum''' SWAP + LAST * 1 - NEG /
ELSE DROP END
≫ ‘'''TanSum'''’ STO
≪ DUP "+" POS → eq op
≪ IF op THEN eq op 1 + OVER SIZE SUB eq 1 op 1 - SUB
ELSE "" eq END
"'" DUP ROT SWAP + + STR→
≫ ≫ ''''PopTerm'''' STO
{} SWAP WHILE DUP "" ≠ REPEAT
'''PopTerm'''
ROT OVER 1 EXGET + SWAP DUP SIZE 1 - EXGET + SWAP
END DROP
≫ ‘'''ParsExp'''’ STO
≪ 1 CF 0
1 '''Formulas''' SIZE FOR f
'''Formulas''' f GET '''ParsExp TanSum'''
IF RND 1 == THEN 1 +
ELSE
1 SF "INCORRECT: π/4 ≠ "
'''Formulas''' f GET + SWAP
END NEXT
→STR IF 1 FS? THEN " others" + END " OK" +
≫ ‘'''TASK'''’ STO
|
'''TanEval''' ''( f c -- tan(c*arctan(f)) )''
if c = 1 then return f
else if c < 0 then return -tan(-c*arctan(f))
else put a = tan((c%2)*arctan(f)) at stack level 3
get b = tan((c-c%2)*arctan(f))
return (a+b)/(-(a*b-1))
'''TanSum''' ''( { c1 f1 .. cn fn } -- result )''
Get tan(c1*arctan(f1))
if input list > 2 items
make { c2 f2 .. cn fn }
evaluate tan of { c2..fn } and add it to
otherwise drop empty list
'''PopTerm''' ''( "T1+T2+..+Tn" -- "T2+..+Tn" 'T1' )''
If input string contains "+" then split it
else string contains only the last term
convert term into algebraic expression
'''ParsExp''' ''( "Machin formula" -- { c1 f1 .. cn fn } )''
Scan the formula
extract next term
extract c and f
Drop empty string
Initialize flag and counter
Scan list of formulas
evaluate tan(formula)
if ok, increase counter
else
build error message
with incorrect formula
Finalize report
|}
{{out}}
<pre>
2: "INCORRECT: π/4 ≠ 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12944)"
1: "16 others OK"
</pre>
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
Line 2,485 ⟶ 2,599:
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|Sidef}}==
{{trans|Python}}
1,151

edits