Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,21 @@
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main(){
int jmax=1000; // maximum value of HIT number. (Length of output file)
int imax=1000; // maximum value of random numbers for producing HITs.
double x,y; // Coordinates
int hit; // storage variable of number of HITs
srand(time(0));
for (int j=0;j<jmax;j++){
hit=0;
x=0; y=0;
for(int i=0;i<imax;i++){
x=double(rand())/double(RAND_MAX);
y=double(rand())/double(RAND_MAX);
if(y<=sqrt(1-pow(x,2))) hit+=1; } //Choosing HITs according to analytic formula of circle
cout<<""<<4*double(hit)/double(imax)<<endl; } // Print out Pi number
}

View file

@ -0,0 +1,15 @@
defmodule MonteCarlo do
def pi(n) do
:random.seed(:os.timestamp)
count = Enum.count(1..n, fn _ ->
x = :random.uniform
y = :random.uniform
:math.sqrt(x*x + y*y) <= 1
end)
4 * count / n
end
end
Enum.each([1000, 10000, 100000, 1000000, 10000000], fn n ->
:io.format "~8w samples: PI = ~f~n", [n, MonteCarlo.pi(n)]
end)

View file

@ -1,3 +1,3 @@
piMC=: monad define "0
4* y%~ +/ 1>: %:+/*: <:+: (2,y) ?@$ 0
4* y%~ +/ 1>: %: +/ *: <: +: (2,y) ?@$ 0
)

View file

@ -1,4 +1,4 @@
my @random_distances := ([+] rand**2 xx 2) xx *;
my @random_distances = ([+] rand**2 xx 2) xx *;
sub approximate_pi(Int $n) {
4 * @random_distances[^$n].grep(* < 1) / $n

View file

@ -1,2 +1,2 @@
my @pi := ([\+] 4 * (1 > [+] rand**2 xx 2) xx *) Z/ 1 .. *;
my @pi = ([\+] 4 * (1 > [+] rand**2 xx 2) xx *) Z/ 1 .. *;
say @pi[10, 1000, 10_000];

View file

@ -1,33 +1,32 @@
/*REXX program computes pi÷4 using the Monte Carlo algorithm. */
parse arg times chunks . /*does user want a specific num? */
if times=='' then times=1000000000 /*one billion should do it. */
if chunks=='' then chunks=10000 /*do Monte Carlo in 10k chunks. */
limit=10000-1 /*REXX random gens only integers.*/
limitSq=limit**2 /*···so, instead of 1, use lim**2*/
!=0 /*number of "pi hits" so far. */
accur=0 /*accuracy of the Monte Carlo pi.*/
if 1=='f1'x then piChar='pi' /*if EBCDIC, then use literal. */
else piChar='e3'x /*if ASCII, then use pi symbol.*/
/*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 /*da real McCoy*/
numeric digits length(pi) /*at least, we'll use these digs.*/
say 'real pi='pi"+" /*might was well brag about it. */
say /*just for the eyeballs. */
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, chunk-at-a-time*/
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 number of repetitions. */
piX=4*!/reps /*let's see how this puppy does. */
_=compare(piX,pi) /*compare apples & ···crabapples.*/
if _<=accur then iterate /*if not better accuracy, pout. */
say right(comma(reps),20) 'repetitions: Monte Carlo' piChar,
"is accurate to" _-1 'places.' /*subtract 1 for dec point.*/
accur=_ /*use this accuracy for baseline.*/
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 done.*/
/*────────────────────────────────COMMA subroutine────────────────────────────────────────────────────────────────────────────────────*/
comma: procedure; parse arg _,c,p,t; arg ,cu; c=word(c ",",1); if cu=='BLANK' then c=' '; o=word(p 3,1); p=abs(o); t=word(t 999999999,1)
if \datatype(p,'W') | \datatype(t,'W') | p==0 | arg()>4 then return _; n=_'.9'; #=123456789; k=0; if o<0 then do; b=verify(_,' ')
if b==0 then return _; e=length(_) - verify(reverse(_),' ') + 1; end; else do;
b=verify(n,#,"M"); e=verify(n,#'0',,verify(n,#"0.",'M'))-p-1; end; do j=e to b by -p while k<t; _=insert(c,_,j); k=k+1; end; return _
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 _