Percolation/Bond percolation: Difference between revisions
Content added Content deleted
m (→{{header|Go}}: User new and more efficient strings.Builder) |
|||
Line 1,151: | Line 1,151: | ||
p = 0.9: 1.00 |
p = 0.9: 1.00 |
||
p = 1.0: 1.00</pre> |
p = 1.0: 1.00</pre> |
||
=={{header|Phix}}== |
|||
<lang Phix> |
|||
--free_console() |
|||
constant w = 10, h = 10 |
|||
sequence wall = join(repeat("+",w+1),"---")&"\n", |
|||
cell = join(repeat("|",w+1)," ")&"\n", |
|||
grid |
|||
procedure new_grid(atom p) |
|||
grid = split(join(repeat(wall,h+1),cell),'\n') |
|||
-- now knock down some walls |
|||
for i=1 to length(grid)-1 do |
|||
integer jstart = 5-mod(i,2)*3, |
|||
jlimit = length(grid[i])-3 |
|||
-- (ie 2..38 on odd lines, 5..37 on even) |
|||
for j=jstart to jlimit by 4 do |
|||
if rnd()>p then |
|||
grid[i][j..j+2] = " " |
|||
end if |
|||
end for |
|||
end for |
|||
end procedure |
|||
function percolate(integer x=0, y=0) |
|||
if x=0 then |
|||
for j=3 to length(grid[1])-2 by 4 do |
|||
if grid[1][j]=' ' and percolate(1,j) then |
|||
return true |
|||
end if |
|||
end for |
|||
elsif grid[x][y]=' ' then |
|||
grid[x][y] = '*' |
|||
if (x=length(grid)-1) |
|||
or ( grid[x+1][y]=' ' and percolate(x+1,y)) |
|||
or (y>6 and grid[x][y-2]=' ' and percolate(x,y-4)) |
|||
or (y<36 and grid[x][y+2]=' ' and percolate(x,y+4)) |
|||
or (x>1 and grid[x-1][y]=' ' and percolate(x-1,y)) then |
|||
return true |
|||
end if |
|||
end if |
|||
return false |
|||
end function |
|||
constant LIM=1000 |
|||
for p=0 to 10 do |
|||
integer count = 0 |
|||
for t=1 to LIM do |
|||
new_grid(p/10) |
|||
count += percolate() |
|||
end for |
|||
printf(1,"p=%.1f: %5.3f\n",{p/10,count/LIM}) |
|||
end for |
|||
puts(1,"sample grid for p=0.6:\n") |
|||
new_grid(0.6) |
|||
{} = percolate() |
|||
puts(1,join(grid,'\n'))</lang> |
|||
{{out}} |
|||
<pre> |
|||
p=0.0: 1.000 |
|||
p=0.1: 1.000 |
|||
p=0.2: 1.000 |
|||
p=0.3: 0.997 |
|||
p=0.4: 0.897 |
|||
p=0.5: 0.434 |
|||
p=0.6: 0.067 |
|||
p=0.7: 0.003 |
|||
p=0.8: 0.000 |
|||
p=0.9: 0.000 |
|||
p=1.0: 0.000 |
|||
sample grid for p=0.6: |
|||
+---+---+ * +---+ * + * +---+---+ * + * + |
|||
| * * * | | * * | * | | * * | |
|||
+---+---+ * + +---+ * + * +---+---+ * + |
|||
| * * | * | | | * | * | * * * | |
|||
+---+ * + * +---+---+---+ * +---+ * +---+ |
|||
| | * | * | * | * | * * * * * | |
|||
+ + * + * + * + * + * +---+ * + * +---+ |
|||
| | * * * * | * * | * * * | |
|||
+ + * + * +---+ * +---+ * +---+ * + * + |
|||
| | * | * * * | * * | * * | * | |
|||
+---+ * +---+ * +---+---+---+---+ * + * + |
|||
| | * | | * * * * | * * | * | |
|||
+ +---+ +---+ * +---+---+---+---+ * + |
|||
| | | | | * | | | * | |
|||
+ +---+---+ +---+---+---+---+---+---+ |
|||
| | | | | | | | | | |
|||
+---+---+---+ + +---+ + +---+---+ |
|||
| | | | | | | | | |
|||
+ +---+ +---+---+ +---+---+---+---+ |
|||
| | | | | | | | | | |
|||
+---+---+ + + +---+---+---+---+ + |
|||
</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |