Data update
This commit is contained in:
parent
81fd053722
commit
52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions
|
|
@ -2,7 +2,7 @@ func average n reps .
|
|||
for r to reps
|
||||
f[] = [ ]
|
||||
for i to n
|
||||
f[] &= randint n
|
||||
f[] &= random n
|
||||
.
|
||||
seen[] = [ ]
|
||||
len seen[] n
|
||||
|
|
|
|||
60
Task/Average-loop-length/XPL0/average-loop-length.xpl0
Normal file
60
Task/Average-loop-length/XPL0/average-loop-length.xpl0
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
include xpllib; \for Print
|
||||
def MAX_N = 20;
|
||||
def TIMES = 1000000;
|
||||
|
||||
func real Factorial_(N);
|
||||
int N;
|
||||
real F;
|
||||
int I;
|
||||
[F:= 1.;
|
||||
for I:= 1 to N do F:= F * float(I);
|
||||
return F;
|
||||
];
|
||||
|
||||
func real Expected(N);
|
||||
int N;
|
||||
real Sum;
|
||||
int I;
|
||||
[Sum:= 0.;
|
||||
for I:= 1 to N do
|
||||
Sum:= Sum + Factorial_(N) / Pow(float(N), float(I)) / Factorial_(N-I);
|
||||
return Sum;
|
||||
];
|
||||
|
||||
func RandInt(N);
|
||||
int N;
|
||||
int R, RMax;
|
||||
def RAND_MAX = -1>>1;
|
||||
[RMax:= RAND_MAX / N * N;
|
||||
repeat R:= Ran(RAND_MAX);
|
||||
until R < RMax;
|
||||
return R / (RAND_MAX / N);
|
||||
];
|
||||
|
||||
func Test(N, Times);
|
||||
int N, Times;
|
||||
int I, Count, X, Bits;
|
||||
[Count:= 0;
|
||||
for I:= 0 to Times-1 do
|
||||
[X:= 1; Bits:= 0;
|
||||
while (Bits & X) = 0 do
|
||||
[Count:= Count+1;
|
||||
Bits:= Bits ! X;
|
||||
X:= 1 << RandInt(N);
|
||||
];
|
||||
];
|
||||
return Count;
|
||||
];
|
||||
|
||||
int N, Cnt;
|
||||
real Avg, Theory, Diff;
|
||||
[\\srand(time(0)); random seeding is automatically done
|
||||
Print(" N\tAvg\tExp.\tDiff\n-------------------------------\n");
|
||||
for N:= 1 to MAX_N do
|
||||
[Cnt:= Test(N, TIMES);
|
||||
Avg:= float(Cnt) / float(TIMES);
|
||||
Theory:= Expected(N);
|
||||
Diff:= (Avg / Theory - 1.) * 100.;
|
||||
Print("%2d %3.4f %3.4f %2.3f%%\n", N, Avg, Theory, Diff);
|
||||
];
|
||||
]
|
||||
41
Task/Average-loop-length/YAMLScript/average-loop-length.ys
Normal file
41
Task/Average-loop-length/YAMLScript/average-loop-length.ys
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
!yamlscript/v0
|
||||
|
||||
# Use *' (vs. *) to allow arbitrary length arithmetic:
|
||||
mul =: value("*'")
|
||||
|
||||
# Number of random times to test each n:
|
||||
TIMES =: 1000000
|
||||
|
||||
defn main(max=20):
|
||||
say: "\tAvg\tExp\tDiff"
|
||||
|
||||
doseq n (1 .. max):
|
||||
anal =: analytical(n):F
|
||||
avg =: avg-cycle-length(n TIMES):F
|
||||
diff =: abs(100 * (1 - (avg / anal)))
|
||||
say:
|
||||
format "%3d\t%.4f\t%.4f\t%.2f%%": n avg anal diff
|
||||
|
||||
defn factorial(n):
|
||||
:: n!
|
||||
range(1 n.++): .mul(*)
|
||||
|
||||
defn analytical(n):
|
||||
:: Analytical computation
|
||||
1 .. n:
|
||||
.map(\(factorial(n) / pow(n _) / factorial(n - _)))
|
||||
.sum()
|
||||
|
||||
defn single-test-cycle-length(n):
|
||||
:: Single random test of cycle length
|
||||
loop count 0, bits 0, x 1:
|
||||
if bit-and(x bits) == 0:
|
||||
recur: count.++ bit-or(bits x) bit-shift-left(1 rand-int(n))
|
||||
else: count
|
||||
|
||||
defn avg-cycle-length(n times):
|
||||
:: Average results of single tests of cycle lengths
|
||||
div _ times:
|
||||
sum:
|
||||
for i (range times):
|
||||
single-test-cycle-length: n
|
||||
Loading…
Add table
Add a link
Reference in a new issue