Check Machin-like formulas: Difference between revisions
Content added Content deleted
(+J (note: I developed & posted this solution entirely on my iPad, so it may have some display issues).) |
(→{{header|J}}: add counterexample) |
||
Line 192: | Line 192: | ||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution''':<lang j> machin =: 1r4p1 = [: +/ ({. * _3 o. %/@:}.)"1@:x:</lang> |
'''Solution''':<lang j> machin =: 1r4p1 = [: +/ ({. * _3 o. %/@:}.)"1@:x:</lang> |
||
'''Example''':<lang j> R =: <@:(0&".);._2 ];._2 noun define |
'''Example''' (''test cases from task description''):<lang j> R =: <@:(0&".);._2 ];._2 noun define |
||
1 1 2 |
1 1 2 |
||
1 1 3 |
1 1 3 |
||
Line 260: | Line 260: | ||
machin&> R |
machin&> R |
||
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1</lang> |
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 |
||
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 first example 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 will maintain extended precision throughout its calculations, as long as it can. |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |