Percolation/Mean cluster density: Difference between revisions

Content added Content deleted
(Added EchoLisp)
(→‎{{header|Tcl}}: added zkl)
Line 877: Line 877:
n=1080, K(p)=0.0661267146776406
n=1080, K(p)=0.0661267146776406
n=6480, K(p)=0.06582889898643499
n=6480, K(p)=0.06582889898643499
</pre>

=={{header|zkl}}==
{{trans|C}}
<lang zkl>const X=-1; // the sentinal that marks an untouched cell
var C,N,NN,P;
fcn createC(n,p){
N,P=n,p; NN=N*N;
C=NN.pump(List.createLong(NN),0); // vector of ints
foreach n in (NN){ C[n]=X*(Float.random(1)<=P) } // X is the sentinal
}
fcn showCluster{
alpha:="-ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz";
foreach n in ([0..NN,N]){ C[n,N].pump(String,alpha.get).println() }
}
fcn countClusters{
clusters:=0;
foreach n in (NN){
if(X!=C[n]) continue;
fcn(n,v){
if((0<=n<NN) and C[n]==X){
C[n]=v;
self.fcn(n-N,v); self.fcn(n-1,v); self.fcn(n+1,v); self.fcn(n+N,v);
}
}(n,clusters+=1);
}
clusters
}
fcn tests(N,n,p){
k:=0.0;
foreach z in (n){ createC(N,p); k+=countClusters().toFloat()/NN; }
k/n
}</lang>
<lang zkl>createC(15,0.5);
println("width=%d, p=%.1f, %d clusters:".fmt(N,P,countClusters()));
showCluster();

println("p=0.5, 5 iterations:");
w:=4; do(6){ println("%5d %9.6f".fmt(w,tests(w, 5, 0.5))); w*=4; }</lang>
{{out}}
<pre>
width=15, p=0.5, 16 clusters:
-AAA-BB-BBB---C
------BBBB--D--
E---F---BB--DD-
EE----G-BB---DD
--H-I--J--J--DD
-K--I--JJ-J--D-
-K--I--JJJJ-L--
KK-III-------MM
-K-I--I--NN-I--
I-IIIII-NNN-III
I-II--I-N-N-II-
III-III--NNN-II
I-II-II-O---I--
I-I-IIII-PP-III
I-II--I---P--II

p=0.5, 5 iterations:
4 0.062500
16 0.070312
64 0.067627
256 0.067078
1024 0.065834
4096 0.065771
</pre>
</pre>