Monte Carlo methods: Difference between revisions
Content added Content deleted
(Added Dart) |
m (→{{header|REXX}}: used a simplier comma function, added/changed whitespace and comments, expanded range of iterations.) |
||
Line 1,887:
=={{header|REXX}}==
A specific-purpose commatizer function is included to format the number of iterations.
<lang rexx>/*REXX program computes and displays the value of pi÷4 using the Monte Carlo algorithm*/
/*true pi*/ pi=3.141592653589793238462643383279502884197169399375105820974944592307816406
say ' 1 2 3 4 5 6 7 '
say 'scale: 1·234567890123456789012345678901234567890123456789012345678901234567890123'
say /* [↑] a two-line scale for showing pi*/
say 'true pi= '
numeric digits length(pi) - 1 /*this program uses these decimal digs.*/
parse arg times chunk . /*does user want a specific number? */
if times=='' | times=="," then times=
if chunk=='' | chunk=="." then chunk= 10000 /*perform Monte Carlo in 10k chunks.*/
limit=10000-1 /*REXX random generates only integers. */
limitSq=limit**2 /*··· so, instead of one, use limit**2.*/
accur=0 /*accuracy of Monte Carlo pi (so far). */
!=0; @reps= 'repetitions: Monte Carlo pi is'
say /*a blank line, just for the eyeballs.*/
do j=1 for times % chunk
do chunk
reps=chunk *
_=compare(4*! / reps, pi) /*compare apples and ··· crabapples. */
if _<=accur then iterate /*if not better accuracy, keep trukin'.*/
say right(
accur=_ /*use this accuracy for next baseline. */
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
'''output''' when using the default inputs:
<pre>
|