Check Machin-like formulas: Difference between revisions

→‎{{header|J}}: add counterexample
(+J (note: I developed & posted this solution entirely on my iPad, so it may have some display issues).)
(→‎{{header|J}}: add counterexample)
Line 192:
=={{header|J}}==
'''Solution''':<lang j> machin =: 1r4p1 = [: +/ ({. * _3 o. %/@:}.)"1@:x:</lang>
'''Example''' (''test cases from task description''):<lang j> R =: <@:(0&".);._2 ];._2 noun define
  1  1     2
  1  1     3
Line 260:
   machin&> R
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1</lang>
'''Example''' (''counterexample''):<lang j>   counterExample=. 12944 (<_1;_1)} >{:R
'''Notes''': The function <tt>machin</tt> compares the results of each formula to π/4 (expressed as <tt>1r4p1</tt> in J's numeric notation). The last expression above shows the results of these comparisons for each formula (with 1 for true and 0 for false). In J, arctan is expressed as <tt>3 o. ''values''</tt> and the function <tt>x:</tt> coerces values to extended precision; J maintains this precision as far as possible.
   counterExample  NB. Same as final test case with 12943 incremented to 12944
88 1   172
51 1   239
32 1   682
44 1  5357
68 1 12944
   machin counterExample
0</lang>
'''Notes''': The function <tt>machin</tt> compares the results of each formula to π/4 (expressed as <tt>1r4p1</tt> in J's numeric notation). The lastfirst expressionexample above shows the results of these comparisons for each formula (with 1 for true and 0 for false). In J, arctan is expressed as <tt>3 o. ''values''</tt> and the function <tt>x:</tt> coerces values to extended precision; thereafter J maintainswill thismaintain extended precision throughout its calculations, as farlong as possibleit can.
 
=={{header|OCaml}}==
Anonymous user