Percolation/Mean cluster density: Difference between revisions

Added Kotlin
(Added Julia language)
(Added Kotlin)
Line 703:
nrep = 5 p = 0.50 dim = 1024 × 1024 sim = 0.06609
nrep = 5 p = 0.50 dim = 4096 × 4096 sim = 0.06588</pre>
 
=={{header|Kotlin}}==
{{trans|C}}
<lang scala>// version 1.2.10
 
import java.util.Random
 
val rand = Random()
const val RAND_MAX = 32767
 
lateinit var map: IntArray
var w = 0
var ww = 0
 
const val ALPHA = "+.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
const val ALEN = ALPHA.length - 3
 
fun makeMap(p: Double) {
val thresh = (p * RAND_MAX).toInt()
ww = w * w
var i = ww
map = IntArray(i)
while (i-- != 0) {
val r = rand.nextInt(RAND_MAX + 1)
if (r < thresh) map[i] = -1
}
}
 
fun showCluster() {
var k = 0
for (i in 0 until w) {
for (j in 0 until w) {
val s = map[k++]
val c = if (s < ALEN) ALPHA[1 + s] else '?'
print(" $c")
}
println()
}
}
 
fun recur(x: Int, v: Int) {
if ((x in 0 until ww) && map[x] == -1) {
map[x] = v
recur(x - w, v)
recur(x - 1, v)
recur(x + 1, v)
recur(x + w, v)
}
}
 
fun countClusters(): Int {
var cls = 0
for (i in 0 until ww) {
if (map[i] != -1) continue
recur(i, ++cls)
}
return cls
}
 
fun tests(n: Int, p: Double): Double {
var k = 0.0
for (i in 0 until n) {
makeMap(p)
k += countClusters().toDouble() / ww
}
return k / n
}
 
fun main(args: Array<String>) {
w = 15
makeMap(0.5)
val cls = countClusters()
println("width = 15, p = 0.5, $cls clusters:")
showCluster()
 
println("\np = 0.5, iter = 5:")
w = 1 shl 2
while (w <= 1 shl 13) {
val t = tests(5, 0.5)
println("%5d %9.6f".format(w, t))
w = w shl 1
}
}</lang>
 
{{out}}
<pre>
width = 15, p = 0.5, 23 clusters:
A . B . C C . . . . D D . D .
. B B B . . D D D . D D . D D
. B . B B B . . D D D . . D .
. . E . . B . B . . D D D D .
F . . B B B B B . G . D . D D
. . B B . B . . H . D D . D D
. B B . . . I . H H . . D D .
B B B . . J . K . . L . D . B
B . . M . . K K K . . D D . B
B B . . . . . K K . . D . B B
B . . N N . . . . . . . O . .
B . . . . P . . O O O O O . .
. . Q . . P . . . O O O O . .
. . . R . . . . S . O . . T T
. U . . . V . . . . . W . . .
 
p = 0.5, iter = 5:
4 0.112500
8 0.121875
16 0.075000
32 0.068750
64 0.068164
128 0.065625
256 0.067093
512 0.065815
1024 0.065863
2048 0.065815
4096 0.065764
8192 0.065766
</pre>
 
=={{header|Perl 6}}==
9,476

edits