Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
6
Task/Monte-Carlo-methods/Jq/monte-carlo-methods-1.jq
Normal file
6
Task/Monte-Carlo-methods/Jq/monte-carlo-methods-1.jq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# In case gojq is used, trim leading 0s:
|
||||
function prng {
|
||||
cat /dev/urandom | tr -cd '0-9' | fold -w 10 | sed 's/^0*\(.*\)*\(.\)*$/\1\2/'
|
||||
}
|
||||
|
||||
prng | jq -nMr -f program.jq
|
||||
24
Task/Monte-Carlo-methods/Jq/monte-carlo-methods-2.jq
Normal file
24
Task/Monte-Carlo-methods/Jq/monte-carlo-methods-2.jq
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
|
||||
|
||||
def percent: "\(100000 * . | round / 1000)%";
|
||||
|
||||
def pi: 4* (1|atan);
|
||||
|
||||
def rfloat: input/1E10;
|
||||
|
||||
def mcPi:
|
||||
. as $n
|
||||
| reduce range(0; $n) as $i (0;
|
||||
rfloat as $x
|
||||
| rfloat as $y
|
||||
| if ($x*$x + $y*$y <= 1) then . + 1 else . end)
|
||||
| 4 * . / $n ;
|
||||
|
||||
"Iterations -> Approx Pi -> Error",
|
||||
"---------- ---------- ------",
|
||||
( pi as $pi
|
||||
| range(1; 7)
|
||||
| pow(10;.) as $p
|
||||
| ($p | mcPi) as $mcpi
|
||||
| ((($pi - $mcpi)|length) / $pi) as $error
|
||||
| "\($p|lpad(10)) \($mcpi|lpad(10)) \($error|percent|lpad(6))" )
|
||||
Loading…
Add table
Add a link
Reference in a new issue