Average loop length: Difference between revisions
Content deleted Content added
Add Factor |
→{{header|Scala}}: migrate to Scala 2.13 |
||
Line 2,184: | Line 2,184: | ||
object AverageLoopLength extends App { |
object AverageLoopLength extends App { |
||
val factorial: |
val factorial: LazyList[Double] = 1 #:: factorial.zip(LazyList.from(1)).map(n => n._2 * factorial(n._2 - 1)) |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
def expected(n: Int) = (for (i <- 1 to n) yield factorial(n) / Math.pow(n, i) / factorial(n - i)).sum |
def expected(n: Int): Double = (for (i <- 1 to n) yield factorial(n) / Math.pow(n, i) / factorial(n - i)).sum |
||
def |
def tested(n: Int, times: Int): Double = (for (i <- 1 to times) yield trial(n)).sum / times |
||
def trial(n: Int): Double = { |
|||
var count = 0 |
var count = 0 |
||
var x = 1 |
var x = 1 |
||
Line 2,200: | Line 2,206: | ||
count |
count |
||
} |
} |
||
def tested(n: Int, times: Int) = (for (i <- 1 to times) yield trial(n)).sum / times |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Line 2,212: | Line 2,211: | ||
println("------------------------------------") |
println("------------------------------------") |
||
results foreach { n => { |
results foreach { n => { |
||
println(f"${n._1}%2d ${n._2}%2.6f ${n._3}%2.6f ${n._4}%2.3f%%") |
|||
} |
|||
} |
} |
||