Percolation/Site percolation: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: syntax coloured)
m (syntax highlighting fixup automation)
Line 22:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">UInt32 seed = 0
F nonrandom()
:seed = 1664525 * :seed + 1013904223
Line 89:
 
L(p, c) sorted(pcount.items())
print(‘#.1: #.’.format(p, c / Float(t)))</langsyntaxhighlight>
 
{{out}}
Line 129:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 193:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 227:
</pre>
{{trans|D}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Line 345:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>Percolating sample (15x15, probability = 0.40):
Line 381:
=={{header|D}}==
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.stdio, std.random, std.array, std.datetime;
 
enum size_t nCols = 15,
Line 475:
writefln("\nSimulations and grid printing performed" ~
" in %3.2f seconds.", sw.peek.msecs / 1000.0);
}</langsyntaxhighlight>
{{out}}
<pre>Percolating sample (15x15, probability = 0.40):
Line 510:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: arrays combinators combinators.short-circuit formatting
fry generalizations io kernel math math.matrices math.order
math.ranges math.vectors prettyprint random sequences ;
Line 564:
] each ;
 
MAIN: site-percolation</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="fortran">
! loosely translated from python.
! compilation: gfortran -Wall -std=f2008 thisfile.f08
Line 724:
 
end program percolation_site
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
{{trans|Phix}}
<langsyntaxhighlight lang="freebasic">#define SOLID "#"
#define EMPTY " "
#define WET "v"
Line 783:
Print Using "p=#.#: #.####"; p; cont/10000
Next ip
Sleep</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 899:
g.use(x-1, y) ||
g.use(x, y-1)
}</langsyntaxhighlight>
{{out}}
<pre>+---------------+
Line 932:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">{-# LANGUAGE OverloadedStrings #-}
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]</langsyntaxhighlight>
 
{{out}}
Line 1,084:
One approach:
 
<langsyntaxhighlight Jlang="j">groups=:[: +/\ 2 </\ 0 , *
ooze=: [ >. [ +&* [ * [: ; groups@[ <@(* * 2 < >./)/. +
percolate=: ooze/\.@|.^:2^:_@(* (1 + # {. 1:))
 
trial=: percolate@([ >: ]?@$0:)
simulate=: %@[ * [: +/ (2 e. {:)@trial&15 15"0@#</langsyntaxhighlight>
 
Example Statistics:
<langsyntaxhighlight Jlang="j"> ,.' P THRU';(, 100&simulate)"0 (i.%<:)11
┌────────┐
│ P THRU│
Line 1,107:
│0.9 1│
│ 1 1│
└────────┘</langsyntaxhighlight>
 
Worked sample:
 
<langsyntaxhighlight Jlang="j"> 1j1 #"1 ' .#'{~ percolate 0.6>:?15 15$0
# # # # # # # #
# # # # # # # # # # # #
Line 1,126:
. . . # . # # #
. . . . . . . # # .
. . . . . . . . # # </langsyntaxhighlight>
 
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">
<lang J>
any =: +./
all =: *./
Line 1,175:
 
simulate =: 100&$: : ([ %~ [: +/ [: percolate"0 #) NB. return fraction of connected cases. Use: T simulate P
</syntaxhighlight>
</lang>
 
<pre>
Line 1,243:
=={{header|Julia}}==
{{trans|Python}}
<langsyntaxhighlight lang="julia">using Printf, Distributions
 
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</langsyntaxhighlight>
 
{{out}}
Line 1,355:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.2.10
 
import java.util.Random
Line 1,421:
println("p = %.1f: %.4f".format(p, cnt / 10000.0))
}
}</langsyntaxhighlight>
 
Sample output:
Line 1,458:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import random, sequtils, strformat, strutils
 
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</langsyntaxhighlight>
 
{{out}}
Line 1,570:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">my $block = '▒';
my $water = '+';
my $pore = ' ';
Line 1,642:
}
 
print "$_\n" for @table;</langsyntaxhighlight>
{{out}}
<pre>Sample percolation at 0.65
Line 1,677:
=={{header|Phix}}==
{{trans|C}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,761:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from random import random
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()})</langsyntaxhighlight>
 
{{out}}
Line 1,872:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang 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)))</langsyntaxhighlight>
 
{{out}}
Line 1,988:
{{works with|Rakudo|2017.02}}
 
<syntaxhighlight lang="raku" perl6line>my $block = '▒';
my $water = '+';
my $pore = ' ';
Line 2,051:
[$x, $y]
}
}</langsyntaxhighlight>
{{out}}
<pre>Sample percolation at .6
Line 2,088:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">class Percolate {
 
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)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,190:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
oo::class create SitePercolation {
Line 2,266:
puts [format "p=%.2f: %2.1f%%" $p [expr {$tot*100./$tries}]]
}
}}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,305:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "random" for Random
import "/fmt" for Fmt
 
Line 2,369:
}
Fmt.print("p = $.1f: $.4f", p, cnt / 10000)
}</langsyntaxhighlight>
 
{{out}}
Line 2,407:
=={{header|zkl}}==
{{trans|C}}
<langsyntaxhighlight lang="zkl">fcn makeGrid(m,n,p){
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();
}</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits