RosettaCodeData/Task/Percolation-Mean-cluster-density/Phix/percolation-mean-cluster-density.phix
2019-09-12 10:33:56 -07:00

66 lines
1.3 KiB
Text

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()