Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
25
Task/Average-loop-length/Factor/average-loop-length.factor
Normal file
25
Task/Average-loop-length/Factor/average-loop-length.factor
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
USING: formatting fry io kernel locals math math.factorials
|
||||
math.functions math.ranges random sequences ;
|
||||
|
||||
: (analytical) ( m n -- x )
|
||||
[ drop factorial ] [ ^ /f ] [ - factorial / ] 2tri ;
|
||||
|
||||
: analytical ( n -- x )
|
||||
dup [1,b] [ (analytical) ] with map-sum ;
|
||||
|
||||
: loop-length ( n -- x )
|
||||
[ 0 0 1 [ 2dup bitand zero? ] ] dip
|
||||
'[ [ 1 + ] 2dip bitor 1 _ random shift ] while 2drop ;
|
||||
|
||||
:: average-loop-length ( n #tests -- x )
|
||||
0 #tests [ n loop-length + ] times #tests / ;
|
||||
|
||||
: stats ( n -- avg exp )
|
||||
[ 1,000,000 average-loop-length ] [ analytical ] bi ;
|
||||
|
||||
: .line ( n -- )
|
||||
dup stats 2dup / 1 - 100 *
|
||||
"%2d %8.4f %8.4f %6.3f%%\n" printf ;
|
||||
|
||||
" n\tavg\texp.\tdiff\n-------------------------------" print
|
||||
20 [1,b] [ .line ] each
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
# Version 5.2
|
||||
using Printf
|
||||
|
||||
analytical(n::Integer) = sum(factorial(n) / big(n) ^ i / factorial(n - i) for i = 1:n)
|
||||
|
||||
function test(n::Integer, times::Integer = 1000000)
|
||||
|
|
|
|||
|
|
@ -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%%")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
22
Task/Average-loop-length/Sidef/average-loop-length.sidef
Normal file
22
Task/Average-loop-length/Sidef/average-loop-length.sidef
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
func find_loop(n) {
|
||||
var seen = Hash()
|
||||
loop {
|
||||
with (irand(1, n)) { |r|
|
||||
seen.has(r) ? (return seen.len) : (seen{r} = true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
print " N empiric theoric (error)\n";
|
||||
print "=== ========= ============ =========\n";
|
||||
|
||||
define MAX = 20
|
||||
define TRIALS = 1000
|
||||
|
||||
for n in (1..MAX) {
|
||||
var empiric = (1..TRIALS -> sum { find_loop(n) } / TRIALS)
|
||||
var theoric = (1..n -> sum {|k| prod(n - k + 1 .. n) * k**2 / n**(k+1) })
|
||||
|
||||
printf("%3d %9.4f %12.4f (%5.2f%%)\n",
|
||||
n, empiric, theoric, 100*(empiric-theoric)/theoric)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue