Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
|
|
@ -2,11 +2,17 @@ import scala.util.Random
|
|||
|
||||
object AverageLoopLength extends App {
|
||||
|
||||
val factorial: Stream[Double] = 1 #:: factorial.zip(Stream.from(1)).map(n => n._2 * factorial(n._2 - 1))
|
||||
val factorial: LazyList[Double] = 1 #:: factorial.zip(LazyList.from(1)).map(n => n._2 * factorial(n._2 - 1))
|
||||
val results = for (n <- 1 to 20;
|
||||
avg = tested(n, 1000000);
|
||||
theory = expected(n)
|
||||
) yield (n, avg, theory, (avg / theory - 1) * 100)
|
||||
|
||||
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 trial(n: Int):Double = {
|
||||
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 x = 1
|
||||
var bits = 0
|
||||
|
|
@ -19,19 +25,12 @@ object AverageLoopLength extends App {
|
|||
count
|
||||
}
|
||||
|
||||
def tested(n: Int, times: Int) = (for (i <- 1 to times) yield trial(n)).sum / times
|
||||
|
||||
val results = for (n <- 1 to 20;
|
||||
avg = tested(n, 1000000);
|
||||
theory = expected(n)
|
||||
) yield (n, avg, theory, (avg / theory - 1) * 100)
|
||||
|
||||
|
||||
println("n avg exp diff")
|
||||
println("------------------------------------")
|
||||
results foreach { n => {
|
||||
println(f"${n._1}%2d ${n._2}%2.6f ${n._3}%2.6f ${n._4}%2.3f%%")
|
||||
}
|
||||
println(f"${n._1}%2d ${n._2}%2.6f ${n._3}%2.6f ${n._4}%2.3f%%")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue