Percolation/Site percolation: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Phix}}: syntax coloured) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 22:
{{trans|Python}}
<
F nonrandom()
:seed = 1664525 * :seed + 1013904223
Line 89:
L(p, c) sorted(pcount.items())
print(‘#.1: #.’.format(p, c / Float(t)))</
{{out}}
Line 129:
=={{header|C}}==
<
#include <stdlib.h>
#include <string.h>
Line 193:
return 0;
}</
{{out}}
<pre>
Line 227:
</pre>
{{trans|D}}
<
#include <stdlib.h>
#include <time.h>
Line 345:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>Percolating sample (15x15, probability = 0.40):
Line 381:
=={{header|D}}==
{{trans|Python}}
<
enum size_t nCols = 15,
Line 475:
writefln("\nSimulations and grid printing performed" ~
" in %3.2f seconds.", sw.peek.msecs / 1000.0);
}</
{{out}}
<pre>Percolating sample (15x15, probability = 0.40):
Line 510:
=={{header|Factor}}==
<
fry generalizations io kernel math math.matrices math.order
math.ranges math.vectors prettyprint random sequences ;
Line 564:
] each ;
MAIN: site-percolation</
{{out}}
<pre>
Line 600:
Please see sample compilation and program execution in comments at top of program. Thank you. This example demonstrates recursion and integer constants of a specific kind.
<
! loosely translated from python.
! compilation: gfortran -Wall -std=f2008 thisfile.f08
Line 724:
end program percolation_site
</syntaxhighlight>
=={{header|FreeBASIC}}==
{{trans|Phix}}
<
#define EMPTY " "
#define WET "v"
Line 783:
Print Using "p=#.#: #.####"; p; cont/10000
Next ip
Sleep</
=={{header|Go}}==
<
import (
Line 899:
g.use(x-1, y) ||
g.use(x, y-1)
}</
{{out}}
<pre>+---------------+
Line 932:
=={{header|Haskell}}==
<
import Control.Monad
import Control.Monad.Random
Line 1,026:
let v = fromIntegral density / fromIntegral densityCount ]
results = zip densities (evalRand tests g2)
mapM_ print [format ("p=" % int % "/" % int % " -> " % fixed 4) density densityCount x | (density,x) <- results]</
{{out}}
Line 1,084:
One approach:
<
ooze=: [ >. [ +&* [ * [: ; groups@[ <@(* * 2 < >./)/. +
percolate=: ooze/\.@|.^:2^:_@(* (1 + # {. 1:))
trial=: percolate@([ >: ]?@$0:)
simulate=: %@[ * [: +/ (2 e. {:)@trial&15 15"0@#</
Example Statistics:
<
┌────────┐
│ P THRU│
Line 1,107:
│0.9 1│
│ 1 1│
└────────┘</
Worked sample:
<
# # # # # # # #
# # # # # # # # # # # #
Line 1,126:
. . . # . # # #
. . . . . . . # # .
. . . . . . . . # # </
An [[Percolation/Site_percolation/J|explanation with examples]] would be somewhat longer than the implementation.
Line 1,132:
Alternative implementation (with an incompatible internal API):
<syntaxhighlight lang="j">
any =: +./
all =: *./
Line 1,175:
simulate =: 100&$: : ([ %~ [: +/ [: percolate"0 #) NB. return fraction of connected cases. Use: T simulate P
</syntaxhighlight>
<pre>
Line 1,243:
=={{header|Julia}}==
{{trans|Python}}
<
newgrid(p::Float64, M::Int=15, N::Int=15) = rand(Bernoulli(p), M, N)
Line 1,317:
for (pi, fi) in zip(p, f)
@printf("p = %.1f ⇛ f = %.3f\n", pi, fi)
end</
{{out}}
Line 1,355:
=={{header|Kotlin}}==
{{trans|C}}
<
import java.util.Random
Line 1,421:
println("p = %.1f: %.4f".format(p, cnt / 10000.0))
}
}</
Sample output:
Line 1,458:
=={{header|Nim}}==
{{trans|Go}}
<
type Grid = seq[string] # Row first, i.e. [y][x].
Line 1,535:
if grid.percolate(): inc count
echo &"p = {p:.2f}: {count / T:.4f}"
p += ΔP</
{{out}}
Line 1,570:
=={{header|Perl}}==
{{trans|Raku}}
<
my $water = '+';
my $pore = ' ';
Line 1,642:
}
print "$_\n" for @table;</
{{out}}
<pre>Sample percolation at 0.65
Line 1,677:
=={{header|Phix}}==
{{trans|C}}
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">grid</span>
Line 1,726:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
{{out}}
<pre>
Line 1,761:
=={{header|Python}}==
<
import string
from pprint import pprint as pp
Line 1,831:
print('\n p: Fraction of %i tries that percolate through\n' % t )
pp({p:c/float(t) for p, c in pcount.items()})</
{{out}}
Line 1,872:
=={{header|Racket}}==
<
(require racket/require (only-in racket/fixnum for*/fxvector))
(require (filtered-in (lambda (name) (regexp-replace #rx"unsafe-" name ""))
Line 1,952:
((i (in-range (t))) #:when (perc-15x15-grid?! (make-15x15-grid p))) 1))
(define proportion-percolated (/ n-percolated-grids (t)))
(printf "p=~a\t->\t~a~%" p (real->decimal-string proportion-percolated 4)))</
{{out}}
Line 1,988:
{{works with|Rakudo|2017.02}}
<syntaxhighlight lang="raku"
my $water = '+';
my $pore = ' ';
Line 2,051:
[$x, $y]
}
}</
{{out}}
<pre>Sample percolation at .6
Line 2,088:
=={{header|Sidef}}==
{{trans|Raku}}
<
has block = '▒'
Line 2,153:
for p in (0.1..1 `by` 0.1) {
printf("p = %0.1f: %0.3f\n", p, tests.of { obj.percolate(p) }.sum / tests)
}</
{{out}}
<pre>
Line 2,190:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<
oo::class create SitePercolation {
Line 2,266:
puts [format "p=%.2f: %2.1f%%" $p [expr {$tot*100./$tries}]]
}
}}</
{{out}}
<pre>
Line 2,305:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 2,369:
}
Fmt.print("p = $.1f: $.4f", p, cnt / 10000)
}</
{{out}}
Line 2,407:
=={{header|zkl}}==
{{trans|C}}
<
grid:=Data((m+1)*(n+1)); // first row and right edges are buffers
grid.write(" "*m); grid.write("\r");
Line 2,435:
cnt:=0.0; do(10000){ cnt+=percolate(makeGrid(15,15,p),15); }
"p=%.1f: %.4f".fmt(p, cnt/10000).println();
}</
{{out}}
<pre>
|