Percolation/Mean cluster density: Difference between revisions

m (→‎{{header|Kotlin}}: Minor adjustments including removal of superfluous whitespace)
Line 914:
𝘱 = 0.5, trials = 5, 𝘕 = 300, 𝘒 = 0.066889
𝘱 = 0.5, trials = 5, 𝘕 = 1000, 𝘒 = 0.0659358
</pre>
 
=={{header|Phix}}==
{{trans|C}}
<lang Phix>sequence grid
integer w, ww
 
procedure make_grid(atom p)
ww = w*w
grid = repeat(0,ww)
for i=1 to ww do
grid[i] = -(rnd()<p)
end for
end procedure
 
constant alpha = "+.ABCDEFGHIJKLMNOPQRSTUVWXYZ"&
"abcdefghijklmnopqrstuvwxyz"
 
procedure show_cluster()
for i=1 to ww do
integer gi = grid[i]+2
grid[i] = iff(gi<=length(alpha)?alpha[gi]:'?')
end for
puts(1,join_by(grid,w,w,""))
end procedure
procedure recur(integer x, v)
if x>=1 and x<=ww and grid[x]==-1 then
grid[x] = v
recur(x-w, v)
recur(x-1, v)
recur(x+1, v)
recur(x+w, v)
end if
end procedure
function count_clusters()
integer cls = 0
for i=1 to ww do
if grid[i]=-1 then
cls += 1
recur(i, cls)
end if
end for
return cls
end function
function tests(int n, atom p)
atom k = 0
for i=1 to n do
make_grid(p)
k += count_clusters()/ww
end for
return k / n
end function
procedure main()
w = 15
make_grid(0.5)
printf(1,"width=15, p=0.5, %d clusters:\n", count_clusters())
show_cluster()
printf(1,"\np=0.5, iter=5:\n")
w = 4
while w<=4096 do
printf(1,"%5d %9.6f\n", {w, tests(5,0.5)})
w *= 4
end while
end procedure
main()</lang>
{{out}}
<pre>
width=15, p=0.5, 18 clusters:
..EE.FF...OO..P
.......KK.OOO.P
A..B..I....OO.P
.BBB.G.LL....PP
BBB...J....J.P.
..BB..JJJJJJ.P.
.BBBB.JJJ.J.J..
BBB....JJJ.JJ.Q
BB.D.D.J.JJJ.QQ
..DDDD.JJ.JJJ.Q
.DDDD.JJ..JJ...
C.DDD.J.N.JJ...
C.D...J..JJ....
.....H...J.....
.......M..O...R
 
p=0.5, iter=5:
4 0.137500
16 0.080469
64 0.068164
256 0.066809
1024 0.066018
4096 0.065777
</pre>
 
7,806

edits