Jump to content

Monte Carlo methods: Difference between revisions

m
→‎{{header|REXX}}: optimized the outer DO loop.
(→‎{{header|REXX}}: added a scale to show the number of (true) decimal digits of pi; added expanded output.)
m (→‎{{header|REXX}}: optimized the outer DO loop.)
Line 1,521:
limit=10000-1 /*REXX random generates only integers. */
limitSq=limit**2 /*··· so, instead of one, use limit**2.*/
!=0; @reps='repetitions: Monte Carlo pi is' /*pi decimal digit accuracy (so far).*/
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
Line 1,528:
if random(0,limit)**2 + random(0,limit)**2 <=limitSq then !=!+1
end /*chunk*/
reps=chunk*j /*computecalculate the number of repetitions. */
piX_=compare(4*! / reps, pi) /*let'scompare seeapples howand this puppy··· does socrabapples. far.*/
if _<=compare(piX, pi) accur then iterate /*compareif applesnot andbetter accuracy, ···keep crabapplestrukin'. */
if _<=accur then iterate /*if not better accuracy, keep going. */
say right(commas(reps),20) @reps 'accurate to' _-1 "places." /*-1 for dec. pt.*/
accur=_ /*use this accuracy for next baseline. */
Cookies help us deliver our services. By using our services, you agree to our use of cookies.