Percolation/Bond percolation: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: syntax coloured)
m (syntax highlighting fixup automation)
Line 27:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">UInt32 seed = 0
F nonrandom()
:seed = 1664525 * :seed + 1013904223
Line 106:
 
L(p, c) sorted(pcount.items())
print(‘#.1: #.’.format(p, c / Float(t)))</langsyntaxhighlight>
 
{{out}}
Line 150:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 249:
free(start);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 289:
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <cstdlib>
#include <cstring>
#include <iostream>
Line 391:
 
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
=={{header|D}}==
{{trans|C}}
<langsyntaxhighlight lang="d">import std.stdio, std.random, std.array, std.range, std.algorithm;
 
struct Grid {
Line 500:
probability, nPercolated / double(nTries));
}
}</langsyntaxhighlight>
{{out}}
<pre>+-+-+-+-+-+-+-+-+-+-+
Line 540:
=={{header|Go}}==
{{trans|C}}<!-- sort of ended up like the C version, not an actual translation -->
<langsyntaxhighlight lang="go">package main
 
import (
Line 671:
}
return false
}</langsyntaxhighlight>
{{out}}
<pre>
Line 708:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">{-# LANGUAGE OverloadedStrings #-}
import Control.Monad
import Control.Monad.Random
Line 828:
let v = fromIntegral density / fromIntegral densityCount ]
let results = zip densities (evalRand tests g2)
mapM_ print [format ("p=" % int % "/" % int % " -> " % fixed 4) density densityCount x | (density,x) <- results]</langsyntaxhighlight>
 
{{out}}
Line 898:
=={{header|Julia}}==
{{trans|Python}}
<langsyntaxhighlight lang="julia">using Printf, Distributions
 
struct Grid
Line 990:
for (pr, pp) in zip(probs, percprobs)
@printf("\tp = %.3f ⇒ freq. = %5.3f\n", pr, pp)
end</langsyntaxhighlight>
 
{{out}}
Line 1,032:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.2.10
 
import java.util.Random
Line 1,122:
println("p = %3g: %.4f".format(pp, cnt.toDouble() / 10_000))
}
}</langsyntaxhighlight>
 
Sample output:
Line 1,163:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import random, sequtils, strformat, tables
 
type
Line 1,254:
if grid.percolate(): inc count
echo &"p = {p:.2f}: {count / T:.3f}"
p += ΔP</langsyntaxhighlight>
 
{{out}}
Line 1,292:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">my @bond;
my $grid = 10;
my $water = '▒';
Line 1,371:
$total += percolate($p) for 1..$tests;
printf "p = %0.1f: %0.2f\n", $p, $total / $tests
}</langsyntaxhighlight>
{{out}}
<pre>Sample percolation at .6
Line 1,410:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">w</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">h</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">10</span>
Line 1,467:
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">percolate</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">grid</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,506:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from collections import namedtuple
from random import random
from pprint import pprint as pp
Line 1,588:
print('\n p: Fraction of %i tries that percolate through' % t )
pp({p:c/float(t) for p, c in pcount.items()})</langsyntaxhighlight>
 
{{out}}
Line 1,636:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
 
(define has-left-wall? (lambda (x) (bitwise-bit-set? x 0)))
Line 1,752:
(make/display/flood/display-bonded-grid 10 10 .25 20)
(make/display/flood/display-bonded-grid 10 10 .50 20)
(make/display/flood/display-bonded-grid 10 10 .75 20000))</langsyntaxhighlight>
 
{{out}}
Line 1,867:
Starts "filling" from the top left. Fluid flow favours directions in Down, Left, Right, Up order. I interpreted p to be porosity, so small p mean low permeability, large p means high permeability.
 
<syntaxhighlight lang="raku" perl6line>my @bond;
my $grid = 10;
my $geom = $grid - 1;
Line 1,938:
sub h () { rand < $prob ?? $sp !! '───' }
sub v () { rand < $prob ?? ' ' !! '│' }
}</langsyntaxhighlight>
{{out}}
<pre>Sample percolation at .6
Line 1,981:
{{trans|C}}
 
<langsyntaxhighlight lang="swift">let randMax = 32767.0
let filled = 1
let rightWall = 2
Line 2,101:
 
print("p = \(porosity): \(Double(count) / 10_000.0)")
}</langsyntaxhighlight>
 
{{out}}
Line 2,141:
{{works with|Tcl|8.6}}
{{trans|Python}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
# Structure the bond percolation system as a class
Line 2,252:
puts [format "p=%.2f: %2.1f%%" $p [expr {$tot*100./$tries}]]
}
}}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,296:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "random" for Random
import "/fmt" for Fmt
 
Line 2,386:
}
Fmt.print("p = $3g: $.4f", pp, cnt / 10000)
}</langsyntaxhighlight>
 
{{out}}
Line 2,428:
=={{header|zkl}}==
{{trans|C}}
<langsyntaxhighlight lang="zkl">// cell states
const FILLED=1; // and odd
const RWALL =2; // right wall
Line 2,473:
i:=0; while(i<m and not fill(grid,i+m,m)){ i+=1; } // pour juice on top row
return(i<m); // percolated through the grid?
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">grid:=makeGrid(10,10,0.40);
println("Did liquid percolate: ",percolate(grid,10)); show(grid,10,10);
 
Line 2,481:
cnt:=0.0; do(10000){ cnt+=percolate(makeGrid(10,10,p),10); }
"p=%.1f: %.4f".fmt(p, cnt/10000).println();
}</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits