2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -3,20 +3,24 @@ where calculating the actual value is difficult or impossible. <br>
It uses random sampling to define constraints on the value
and then makes a sort of "best guess."
A simple Monte Carlo Simulation can be used to calculate the value for π.
A simple Monte Carlo Simulation can be used to calculate the value for <big><math>\pi</math></big>.
If you had a circle and a square where the length of a side of the square
was the same as the diameter of the circle, the ratio of the area of the circle
to the area of the square would be π/4.
to the area of the square would be <big><math>\pi/4</math></big>.
So, if you put this circle inside the square and select many random points
inside the square, the number of points inside the circle
divided by the number of points inside the square and the circle
would be approximately π/4.
would be approximately <big><math>\pi/4</math></big>.
;Task:
Write a function to run a simulation like this, with a variable number of random points to select.
Write a function to run a simulation like this, with a variable number
of random points to select. <br>
Also, show the results of a few different sample sizes.
For software where the number π is not built-in,
we give π to a couple of digits:
3.141592653589793238462643383280
For software where the number <big><math>\pi</math></big> is not built-in,
we give <big><math>\pi</math></big> as a number of digits:
3.141592653589793238462643383280
<br><br>

View file

@ -1,9 +1,8 @@
defmodule MonteCarlo do
def pi(n) do
:random.seed(:os.timestamp)
count = Enum.count(1..n, fn _ ->
x = :random.uniform
y = :random.uniform
x = :rand.uniform
y = :rand.uniform
:math.sqrt(x*x + y*y) <= 1
end)
4 * count / n

View file

@ -0,0 +1,16 @@
program mc
integer :: n,i
real(8) :: pi
n=10000
do i=1,5
print*,n,pi(n)
n = n * 10
end do
end program
function pi(n)
integer :: n
real(8) :: x(2,n),pi
call random_number(x)
pi = 4.d0 * dble( count( hypot(x(1,:),x(2,:)) <= 1.d0 ) ) / n
end function

View file

@ -1,32 +1,30 @@
/*REXX program computes pi ÷ 4 using the Monte Carlo algorithm. */
parse arg times chunks . /*does user want a specific number? */
if times=='' then times=1000000000 /*one billion should do it, me thinks. */
if chunks=='' then chunks=10000 /*do Monte Carlo in 10,000 chunks. */
limit=10000-1 /*REXX random generates only integers. */
limitSq=limit**2 /*··· so, instead of one, use limit**2.*/
!=0 /*the number of "pi hits" (so far). */
accur=0 /*accuracy of Monte Carlo pi (so far). */
if 1=='f1'x then piChar='pi' /*if EBCDIC, then use literal. */
else piChar='e3'x /* " ASCII, " " pi glyph, */
pi=3.14159265358979323846264338327950288419716939937511 /*this, da real McCoy*/
numeric digits length(pi) /*this program uses these decimal digs.*/
say 'real pi='pi"+" /*we might as well brag about it. */
say /*a blank line, just for the eyeballs. */
do j=1 for times%chunks
do chunks /*do Monte Carlo, one chunk-at-a-time.*/
if random(0,limit)**2 + random(0,limit)**2 <=limitSq then !=!+1
end /*chunks*/
reps=chunks*j /*compute the number of repetitions. */
piX=4*!/reps /*let's see how this puppy does so far.*/
_=compare(piX,pi) /*compare apples and ··· crabapples. */
if _<=accur then iterate /*if not better accuracy, keep going. */
say right(commas(reps),20) 'repetitions: Monte Carlo' piChar,
"is accurate to" _-1 'places.' /*subtract one for decimal point.*/
accur=_ /*use this accuracy for the baseline. */
end /*j*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
commas: procedure; parse arg _; n=_'.9'; #=123456789; b=verify(n,#,"M")
e=verify(n,#'0',,verify(n,#"0.",'M'))-4
do j=e to b by -3; _=insert(',',_,j); end /*j*/; return _
/*REXX program computes and displays the value of pi÷4 using the Monte Carlo algorithm*/
pi=3.141592653589793238462643383279502884197169399375105820974944592307816406 /*true pi.*/
say ' 1 2 3 4 5 6 7 '
say 'scale: 1·234567890123456789012345678901234567890123456789012345678901234567890123'
say /* [↑] a two-line scale for showing pi*/
say 'true pi='pi"+" /*we might as well brag about 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=1000000000 /*one 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 not better accuracy, keep trukin'.*/
say right(commas(reps),20) @reps 'accurate to' _-1 "places." /*-1 for dec. pt.*/
accur=_ /*use this accuracy for next baseline. */
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: procedure; parse arg _; n=_'.9'; #=123456789; b=verify(n,#,"M")
e=verify(n, #'0', , verify(n, #"0.", 'M') ) - 4
do j=e to b by -3; _=insert(',',_,j); end /*j*/; return _