Monte Carlo methods: Difference between revisions
Content deleted Content added
m →{{header|F#}}: edited name |
m →{{header|REXX}}: optimized the inner DO loop. |
||
Line 1,929: | Line 1,929: | ||
if times=='' | times=="," then times=5000000000 /*five billion should do it, hopefully.*/ |
if times=='' | times=="," then times=5000000000 /*five billion should do it, hopefully.*/ |
||
if chunk=='' | chunk=="." then chunk= 10000 /*perform Monte Carlo in 10k chunks.*/ |
if chunk=='' | chunk=="." then chunk= 10000 /*perform Monte Carlo in 10k chunks.*/ |
||
limit=10000 |
limit=10000 /*REXX random generates only integers. */ |
||
limitSq=limit**2 /*··· so, instead of one, use limit**2.*/ |
limitSq=limit**2 /*··· so, instead of one, use limit**2.*/ |
||
accur=0 /*accuracy of Monte Carlo pi (so far). |
accur=0 /*accuracy of Monte Carlo pi (so far).*/ |
||
!=0; @reps= 'repetitions: Monte Carlo pi is' /*pi decimal digit accuracy (so far).*/ |
!=0; @reps= 'repetitions: Monte Carlo pi is' /*pi decimal digit accuracy (so far).*/ |
||
say /*a blank line, just for the eyeballs.*/ |
say /*a blank line, just for the eyeballs.*/ |
||
do j=1 for times % chunk |
do j=1 for times % chunk |
||
do chunk /*do Monte Carlo, one chunk at-a-time.*/ |
do chunk /*do Monte Carlo, one chunk at-a-time.*/ |
||
if random( |
if random(, limit)**2 + random(, limit)**2 <= limitSq then !=! + 1 |
||
end /*chunk*/ |
end /*chunk*/ |
||
reps=chunk * j /*calculate the number of repetitions. */ |
reps=chunk * j /*calculate the number of repetitions. */ |
||
_=compare(4*! / reps, pi) /*compare apples and ··· crabapples. */ |
_=compare(4*! / reps, pi) /*compare apples and ··· crabapples. */ |
||
if _<=accur then iterate /* |
if _<=accur then iterate /*Not better accuracy? Keep truckin'. */ |
||
say right(comma(reps), 20) @reps 'accurate to' _-1 "places." /* |
say right(comma(reps), 20) @reps 'accurate to' _-1 "places." /*─1 ≡ dec. point*/ |
||
accur=_ /*use this accuracy for next baseline. */ |
accur=_ /*use this accuracy for next baseline. */ |
||
end /*j*/ |
end /*j*/ |