Percolation/Site percolation: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: syntax coloured)
m (syntax highlighting fixup automation)
Line 22: Line 22:
{{trans|Python}}
{{trans|Python}}


<lang 11l>UInt32 seed = 0
<syntaxhighlight lang="11l">UInt32 seed = 0
F nonrandom()
F nonrandom()
:seed = 1664525 * :seed + 1013904223
:seed = 1664525 * :seed + 1013904223
Line 89: Line 89:


L(p, c) sorted(pcount.items())
L(p, c) sorted(pcount.items())
print(‘#.1: #.’.format(p, c / Float(t)))</lang>
print(‘#.1: #.’.format(p, c / Float(t)))</syntaxhighlight>


{{out}}
{{out}}
Line 129: Line 129:


=={{header|C}}==
=={{header|C}}==
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
Line 193: Line 193:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 227: Line 227:
</pre>
</pre>
{{trans|D}}
{{trans|D}}
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <time.h>
#include <time.h>
Line 345: Line 345:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>Percolating sample (15x15, probability = 0.40):
<pre>Percolating sample (15x15, probability = 0.40):
Line 381: Line 381:
=={{header|D}}==
=={{header|D}}==
{{trans|Python}}
{{trans|Python}}
<lang d>import std.stdio, std.random, std.array, std.datetime;
<syntaxhighlight lang="d">import std.stdio, std.random, std.array, std.datetime;


enum size_t nCols = 15,
enum size_t nCols = 15,
Line 475: Line 475:
writefln("\nSimulations and grid printing performed" ~
writefln("\nSimulations and grid printing performed" ~
" in %3.2f seconds.", sw.peek.msecs / 1000.0);
" in %3.2f seconds.", sw.peek.msecs / 1000.0);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Percolating sample (15x15, probability = 0.40):
<pre>Percolating sample (15x15, probability = 0.40):
Line 510: Line 510:


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: arrays combinators combinators.short-circuit formatting
<syntaxhighlight lang="factor">USING: arrays combinators combinators.short-circuit formatting
fry generalizations io kernel math math.matrices math.order
fry generalizations io kernel math math.matrices math.order
math.ranges math.vectors prettyprint random sequences ;
math.ranges math.vectors prettyprint random sequences ;
Line 564: Line 564:
] each ;
] each ;


MAIN: site-percolation</lang>
MAIN: site-percolation</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 600: 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.
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.
<lang fortran>
<syntaxhighlight lang="fortran">
! loosely translated from python.
! loosely translated from python.
! compilation: gfortran -Wall -std=f2008 thisfile.f08
! compilation: gfortran -Wall -std=f2008 thisfile.f08
Line 724: Line 724:


end program percolation_site
end program percolation_site
</syntaxhighlight>
</lang>


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
{{trans|Phix}}
{{trans|Phix}}
<lang freebasic>#define SOLID "#"
<syntaxhighlight lang="freebasic">#define SOLID "#"
#define EMPTY " "
#define EMPTY " "
#define WET "v"
#define WET "v"
Line 783: Line 783:
Print Using "p=#.#: #.####"; p; cont/10000
Print Using "p=#.#: #.####"; p; cont/10000
Next ip
Next ip
Sleep</lang>
Sleep</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 899: Line 899:
g.use(x-1, y) ||
g.use(x-1, y) ||
g.use(x, y-1)
g.use(x, y-1)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>+---------------+
<pre>+---------------+
Line 932: Line 932:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>{-# LANGUAGE OverloadedStrings #-}
<syntaxhighlight lang="haskell">{-# LANGUAGE OverloadedStrings #-}
import Control.Monad
import Control.Monad
import Control.Monad.Random
import Control.Monad.Random
Line 1,026: Line 1,026:
let v = fromIntegral density / fromIntegral densityCount ]
let v = fromIntegral density / fromIntegral densityCount ]
results = zip densities (evalRand tests g2)
results = zip densities (evalRand tests g2)
mapM_ print [format ("p=" % int % "/" % int % " -> " % fixed 4) density densityCount x | (density,x) <- results]</lang>
mapM_ print [format ("p=" % int % "/" % int % " -> " % fixed 4) density densityCount x | (density,x) <- results]</syntaxhighlight>


{{out}}
{{out}}
Line 1,084: Line 1,084:
One approach:
One approach:


<lang J>groups=:[: +/\ 2 </\ 0 , *
<syntaxhighlight lang="j">groups=:[: +/\ 2 </\ 0 , *
ooze=: [ >. [ +&* [ * [: ; groups@[ <@(* * 2 < >./)/. +
ooze=: [ >. [ +&* [ * [: ; groups@[ <@(* * 2 < >./)/. +
percolate=: ooze/\.@|.^:2^:_@(* (1 + # {. 1:))
percolate=: ooze/\.@|.^:2^:_@(* (1 + # {. 1:))


trial=: percolate@([ >: ]?@$0:)
trial=: percolate@([ >: ]?@$0:)
simulate=: %@[ * [: +/ (2 e. {:)@trial&15 15"0@#</lang>
simulate=: %@[ * [: +/ (2 e. {:)@trial&15 15"0@#</syntaxhighlight>


Example Statistics:
Example Statistics:
<lang J> ,.' P THRU';(, 100&simulate)"0 (i.%<:)11
<syntaxhighlight lang="j"> ,.' P THRU';(, 100&simulate)"0 (i.%<:)11
┌────────┐
┌────────┐
│ P THRU│
│ P THRU│
Line 1,107: Line 1,107:
│0.9 1│
│0.9 1│
│ 1 1│
│ 1 1│
└────────┘</lang>
└────────┘</syntaxhighlight>


Worked sample:
Worked sample:


<lang J> 1j1 #"1 ' .#'{~ percolate 0.6>:?15 15$0
<syntaxhighlight lang="j"> 1j1 #"1 ' .#'{~ percolate 0.6>:?15 15$0
# # # # # # # #
# # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # #
Line 1,126: Line 1,126:
. . . # . # # #
. . . # . # # #
. . . . . . . # # .
. . . . . . . # # .
. . . . . . . . # # </lang>
. . . . . . . . # # </syntaxhighlight>


An [[Percolation/Site_percolation/J|explanation with examples]] would be somewhat longer than the implementation.
An [[Percolation/Site_percolation/J|explanation with examples]] would be somewhat longer than the implementation.
Line 1,132: Line 1,132:
Alternative implementation (with an incompatible internal API):
Alternative implementation (with an incompatible internal API):


<syntaxhighlight lang="j">
<lang J>
any =: +./
any =: +./
all =: *./
all =: *./
Line 1,175: Line 1,175:


simulate =: 100&$: : ([ %~ [: +/ [: percolate"0 #) NB. return fraction of connected cases. Use: T simulate P
simulate =: 100&$: : ([ %~ [: +/ [: percolate"0 #) NB. return fraction of connected cases. Use: T simulate P
</syntaxhighlight>
</lang>


<pre>
<pre>
Line 1,243: Line 1,243:
=={{header|Julia}}==
=={{header|Julia}}==
{{trans|Python}}
{{trans|Python}}
<lang julia>using Printf, Distributions
<syntaxhighlight lang="julia">using Printf, Distributions


newgrid(p::Float64, M::Int=15, N::Int=15) = rand(Bernoulli(p), M, N)
newgrid(p::Float64, M::Int=15, N::Int=15) = rand(Bernoulli(p), M, N)
Line 1,317: Line 1,317:
for (pi, fi) in zip(p, f)
for (pi, fi) in zip(p, f)
@printf("p = %.1f ⇛ f = %.3f\n", pi, fi)
@printf("p = %.1f ⇛ f = %.3f\n", pi, fi)
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 1,355: Line 1,355:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|C}}
{{trans|C}}
<lang scala>// version 1.2.10
<syntaxhighlight lang="scala">// version 1.2.10


import java.util.Random
import java.util.Random
Line 1,421: Line 1,421:
println("p = %.1f: %.4f".format(p, cnt / 10000.0))
println("p = %.1f: %.4f".format(p, cnt / 10000.0))
}
}
}</lang>
}</syntaxhighlight>


Sample output:
Sample output:
Line 1,458: Line 1,458:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Go}}
{{trans|Go}}
<lang Nim>import random, sequtils, strformat, strutils
<syntaxhighlight lang="nim">import random, sequtils, strformat, strutils


type Grid = seq[string] # Row first, i.e. [y][x].
type Grid = seq[string] # Row first, i.e. [y][x].
Line 1,535: Line 1,535:
if grid.percolate(): inc count
if grid.percolate(): inc count
echo &"p = {p:.2f}: {count / T:.4f}"
echo &"p = {p:.2f}: {count / T:.4f}"
p += ΔP</lang>
p += ΔP</syntaxhighlight>


{{out}}
{{out}}
Line 1,570: Line 1,570:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>my $block = '▒';
<syntaxhighlight lang="perl">my $block = '▒';
my $water = '+';
my $water = '+';
my $pore = ' ';
my $pore = ' ';
Line 1,642: Line 1,642:
}
}


print "$_\n" for @table;</lang>
print "$_\n" for @table;</syntaxhighlight>
{{out}}
{{out}}
<pre>Sample percolation at 0.65
<pre>Sample percolation at 0.65
Line 1,677: Line 1,677:
=={{header|Phix}}==
=={{header|Phix}}==
{{trans|C}}
{{trans|C}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<span style="color: #004080;">string</span> <span style="color: #000000;">grid</span>
Line 1,726: Line 1,726:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,761: Line 1,761:


=={{header|Python}}==
=={{header|Python}}==
<lang python>from random import random
<syntaxhighlight lang="python">from random import random
import string
import string
from pprint import pprint as pp
from pprint import pprint as pp
Line 1,831: Line 1,831:
print('\n p: Fraction of %i tries that percolate through\n' % t )
print('\n p: Fraction of %i tries that percolate through\n' % t )
pp({p:c/float(t) for p, c in pcount.items()})</lang>
pp({p:c/float(t) for p, c in pcount.items()})</syntaxhighlight>


{{out}}
{{out}}
Line 1,872: Line 1,872:


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(require racket/require (only-in racket/fixnum for*/fxvector))
(require racket/require (only-in racket/fixnum for*/fxvector))
(require (filtered-in (lambda (name) (regexp-replace #rx"unsafe-" name ""))
(require (filtered-in (lambda (name) (regexp-replace #rx"unsafe-" name ""))
Line 1,952: Line 1,952:
((i (in-range (t))) #:when (perc-15x15-grid?! (make-15x15-grid p))) 1))
((i (in-range (t))) #:when (perc-15x15-grid?! (make-15x15-grid p))) 1))
(define proportion-percolated (/ n-percolated-grids (t)))
(define proportion-percolated (/ n-percolated-grids (t)))
(printf "p=~a\t->\t~a~%" p (real->decimal-string proportion-percolated 4)))</lang>
(printf "p=~a\t->\t~a~%" p (real->decimal-string proportion-percolated 4)))</syntaxhighlight>


{{out}}
{{out}}
Line 1,988: Line 1,988:
{{works with|Rakudo|2017.02}}
{{works with|Rakudo|2017.02}}


<lang perl6>my $block = '▒';
<syntaxhighlight lang="raku" line>my $block = '▒';
my $water = '+';
my $water = '+';
my $pore = ' ';
my $pore = ' ';
Line 2,051: Line 2,051:
[$x, $y]
[$x, $y]
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Sample percolation at .6
<pre>Sample percolation at .6
Line 2,088: Line 2,088:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Raku}}
{{trans|Raku}}
<lang ruby>class Percolate {
<syntaxhighlight lang="ruby">class Percolate {


has block = '▒'
has block = '▒'
Line 2,153: Line 2,153:
for p in (0.1..1 `by` 0.1) {
for p in (0.1..1 `by` 0.1) {
printf("p = %0.1f: %0.3f\n", p, tests.of { obj.percolate(p) }.sum / tests)
printf("p = %0.1f: %0.3f\n", p, tests.of { obj.percolate(p) }.sum / tests)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,190: Line 2,190:
=={{header|Tcl}}==
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
{{works with|Tcl|8.6}}
<lang tcl>package require Tcl 8.6
<syntaxhighlight lang="tcl">package require Tcl 8.6


oo::class create SitePercolation {
oo::class create SitePercolation {
Line 2,266: Line 2,266:
puts [format "p=%.2f: %2.1f%%" $p [expr {$tot*100./$tries}]]
puts [format "p=%.2f: %2.1f%%" $p [expr {$tot*100./$tries}]]
}
}
}}</lang>
}}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,305: Line 2,305:
{{trans|Kotlin}}
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "random" for Random
<syntaxhighlight lang="ecmascript">import "random" for Random
import "/fmt" for Fmt
import "/fmt" for Fmt


Line 2,369: Line 2,369:
}
}
Fmt.print("p = $.1f: $.4f", p, cnt / 10000)
Fmt.print("p = $.1f: $.4f", p, cnt / 10000)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,407: Line 2,407:
=={{header|zkl}}==
=={{header|zkl}}==
{{trans|C}}
{{trans|C}}
<lang zkl>fcn makeGrid(m,n,p){
<syntaxhighlight lang="zkl">fcn makeGrid(m,n,p){
grid:=Data((m+1)*(n+1)); // first row and right edges are buffers
grid:=Data((m+1)*(n+1)); // first row and right edges are buffers
grid.write(" "*m); grid.write("\r");
grid.write(" "*m); grid.write("\r");
Line 2,435: Line 2,435:
cnt:=0.0; do(10000){ cnt+=percolate(makeGrid(15,15,p),15); }
cnt:=0.0; do(10000){ cnt+=percolate(makeGrid(15,15,p),15); }
"p=%.1f: %.4f".fmt(p, cnt/10000).println();
"p=%.1f: %.4f".fmt(p, cnt/10000).println();
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>