Monte Carlo methods: Difference between revisions

m
(Added solution for EDSAC)
m (→‎{{header|Wren}}: Minor tidy)
 
(5 intermediate revisions by 3 users not shown)
Line 1,022:
 
=={{header|EasyLang}}==
<syntaxhighlight lang="text">func mc n . .
func mc n .
for i range n
for xi = randomf1 to n
y x = randomf
if x * x + y * y <= 1randomf
hitif x * x += y * y < 1
. hit += 1
.
.
print return 4.0 * hit / n
.
numfmt 4 0
fscale 4
callprint mc 10000
callprint mc 100000
callprint mc 1000000
callprint mc 10000000</syntaxhighlight>
</syntaxhighlight>
Output:
3.1292
Line 1,731 ⟶ 1,733:
piMC 10^i.7
4 2.8 3.24 3.168 3.1432 3.14256 3.14014</syntaxhighlight>
 
'''Alternative Tacit Solution:'''
<syntaxhighlight lang="j">pimct=. (4 * +/ % #)@:(1 >: |)@:(? j. ?)@:($&0)"0
(,. pimct) 10 ^ 3 + i.6
1000 3.168
10000 3.122
100000 3.13596
1e6 3.1428
1e7 3.14158
1e8 3.14154</syntaxhighlight>
 
=={{header|Java}}==
Line 2,819 ⟶ 2,832:
 
=={{header|RPL}}==
≪ 0 1 3 PICK '''START'''
RAND SQ RAND SQ '''IF''' + 1 <3 '''THEN''' 1 +PICK '''END NEXTSTART'''
RAND SQ RAND SQ + 1 < +
SWAP / 4 *
'''NEXT'MCARL'''' STO
SWAP / 4 *
≫ '<span style="color:blue">MCARL</span>' STO
 
100 '''<span style="color:blue">MCARL'''</span>
1000 <span style="color:blue">MCARL</span>
10000 '''MCARL'''
10000 '''<span style="color:blue">MCARL'''</span>
100000 <span style="color:blue">MCARL</span>
500000 '''MCARL'''
{{out}}
<pre>
4: 3.162
3: 3.166084
2: 3.12281684
1: 3.14371214154
</pre>
 
Line 3,204 ⟶ 3,219:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./fmt" for Fmt
 
var rand = Random.new()
9,482

edits