Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
23
Task/Monte-Carlo-methods/Kotlin/monte-carlo-methods.kotlin
Normal file
23
Task/Monte-Carlo-methods/Kotlin/monte-carlo-methods.kotlin
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
// version 1.1.0
|
||||
|
||||
fun mcPi(n: Int): Double {
|
||||
var inside = 0
|
||||
(1..n).forEach {
|
||||
val x = Math.random()
|
||||
val y = Math.random()
|
||||
if (x * x + y * y <= 1.0) inside++
|
||||
}
|
||||
return 4.0 * inside / n
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
println("Iterations -> Approx Pi -> Error%")
|
||||
println("---------- ---------- ------")
|
||||
var n = 1_000
|
||||
while (n <= 100_000_000) {
|
||||
val pi = mcPi(n)
|
||||
val err = Math.abs(Math.PI - pi) / Math.PI * 100.0
|
||||
println(String.format("%9d -> %10.8f -> %6.4f", n, pi, err))
|
||||
n *= 10
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue