Percolation/Site percolation: Difference between revisions

m
(Added Haskell version)
Line 814:
 
=={{header|J}}==
 
One approach:
 
<lang J>groups=:[: +/\ 2 </\ 0 , *
ooze=: [ >. [ +&* [ * [: ; groups@[ <@(* * 2 < >./)/. +
percolate=: ooze/\.@|.^:2^:_@(* (1 + # {. 1:))
 
trial=: percolate@([ >: ]?@$0:)
simulate=: %@[ * [: +/ (2 e. {:)@trial&15 15"0@#</lang>
 
Example Statistics:
<lang J> ,.' P THRU';(, 100&simulate)"0 (i.%<:)11
┌────────┐
│ P THRU│
├────────┤
│ 0 0│
│0.1 0│
│0.2 0│
│0.3 0│
│0.4 0.01│
│0.5 0.09│
│0.6 0.61│
│0.7 0.97│
│0.8 1│
│0.9 1│
│ 1 1│
└────────┘</lang>
 
Worked sample:
 
<lang J> 1j1 #"1 ' .#'{~ percolate 0.6>:?15 15$0
# # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # # # #
# # # . # #
# # # . # # # # # # #
# # # # # # # # #
# # . . . # # #
. . . # # # . #
. . . # # # # # #
. . . . . # # # # # # # # #
. . . . # # # # #
. . . . . . # # # .
. . . . . # .
. . . . . . . . .
. . . . . . . . . . </lang>
 
Alternative implementation (with an incompatible internal API):
 
<lang J>
any =: +./
6,962

edits