Monte Carlo methods: Difference between revisions

m
→‎{{header|REXX}}: optimized the inner DO loop.
m (→‎{{header|F#}}: edited name)
m (→‎{{header|REXX}}: optimized the inner DO loop.)
Line 1,929:
if times=='' | times=="," then times=5000000000 /*five billion should do it, hopefully.*/
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' /*pi decimal digit accuracy (so far).*/
say /*a blank line, just for the eyeballs.*/
do j=1 for times % chunk
do chunk /*do Monte Carlo, one chunk at-a-time.*/
if random(0, limit)**2 + random(0, limit)**2 <= limitSq then !=! + 1
end /*chunk*/
reps=chunk * j /*calculate the number of repetitions. */
_=compare(4*! / reps, pi) /*compare apples and ··· crabapples. */
if _<=accur then iterate /*if notNot better accuracy,? keep trukinKeep truckin'. */
say right(comma(reps), 20) @reps 'accurate to' _-1 "places." /*-1─1 for dec. pt.point*/
accur=_ /*use this accuracy for next baseline. */
end /*j*/