June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
62
Task/Average-loop-length/C++/average-loop-length.cpp
Normal file
62
Task/Average-loop-length/C++/average-loop-length.cpp
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
#include <random>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
#define MAX_N 20
|
||||
#define TIMES 1000000
|
||||
|
||||
/**
|
||||
* Used to generate a uniform random distribution
|
||||
*/
|
||||
static std::random_device rd; //Will be used to obtain a seed for the random number engine
|
||||
static std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
|
||||
static std::uniform_int_distribution<> dis;
|
||||
|
||||
int randint(int n) {
|
||||
int r, rmax = RAND_MAX / n * n;
|
||||
dis=std::uniform_int_distribution(0,rmax) ;
|
||||
r = dis(gen);
|
||||
return r / (RAND_MAX / n);
|
||||
}
|
||||
|
||||
unsigned long factorial(size_t n) {
|
||||
//Factorial using dynamic programming to memoize the values.
|
||||
static std::vector<unsigned long>factorials{1,1,2};
|
||||
for (;factorials.size() <= n;)
|
||||
factorials.push_back(factorials.back()*factorials.size());
|
||||
return factorials[n];
|
||||
}
|
||||
|
||||
long double expected(size_t n) {
|
||||
long double sum = 0;
|
||||
for (size_t i = 1; i <= n; i++)
|
||||
sum += factorial(n) / pow(n, i) / factorial(n - i);
|
||||
return sum;
|
||||
}
|
||||
|
||||
int test(int n, int times) {
|
||||
int i, count = 0;
|
||||
for (i = 0; i < times; i++) {
|
||||
unsigned int x = 1, bits = 0;
|
||||
while (!(bits & x)) {
|
||||
count++;
|
||||
bits |= x;
|
||||
x = static_cast<unsigned int>(1 << randint(n));
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int main() {
|
||||
puts(" n\tavg\texp.\tdiff\n-------------------------------");
|
||||
|
||||
int n;
|
||||
for (n = 1; n <= MAX_N; n++) {
|
||||
int cnt = test(n, TIMES);
|
||||
long double avg = (double)cnt / TIMES;
|
||||
long double theory = expected(static_cast<size_t>(n));
|
||||
long double diff = (avg / theory - 1) * 100;
|
||||
printf("%2d %8.4f %8.4f %6.3f%%\n", n, static_cast<double>(avg), static_cast<double>(theory), static_cast<double>(diff));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
MODULE AvgLoopLen;
|
||||
(* Oxford Oberon-2 *)
|
||||
IMPORT Random, Out;
|
||||
|
||||
PROCEDURE Fac(n: INTEGER; f: REAL): REAL;
|
||||
BEGIN
|
||||
IF n = 0 THEN
|
||||
RETURN f
|
||||
ELSE
|
||||
RETURN Fac(n - 1,n*f)
|
||||
END
|
||||
END Fac;
|
||||
|
||||
PROCEDURE Power(n,i: INTEGER): REAL;
|
||||
VAR
|
||||
p: REAL;
|
||||
BEGIN
|
||||
p := 1.0;
|
||||
WHILE i > 0 DO p := p * n; DEC(i) END;
|
||||
RETURN p
|
||||
END Power;
|
||||
|
||||
PROCEDURE Abs(x: REAL): REAL;
|
||||
BEGIN
|
||||
IF x < 0 THEN RETURN -x ELSE RETURN x END
|
||||
END Abs;
|
||||
|
||||
PROCEDURE Analytical(n: INTEGER): REAL;
|
||||
VAR
|
||||
i: INTEGER;
|
||||
res: REAL;
|
||||
BEGIN
|
||||
res := 0.0;
|
||||
FOR i := 1 TO n DO
|
||||
res := res + (Fac(n,1.0) / Power(n,i) / Fac(n - i,1.0));
|
||||
END;
|
||||
RETURN res
|
||||
END Analytical;
|
||||
|
||||
PROCEDURE Averages(n: INTEGER): REAL;
|
||||
CONST
|
||||
times = 100000;
|
||||
VAR
|
||||
rnds: SET;
|
||||
r,count,i: INTEGER;
|
||||
BEGIN
|
||||
count := 0; i := 0;
|
||||
WHILE i < times DO
|
||||
rnds := {};
|
||||
LOOP
|
||||
r := Random.Roll(n);
|
||||
IF r IN rnds THEN EXIT ELSE INCL(rnds,r); INC(count) END
|
||||
END;
|
||||
INC(i)
|
||||
END;
|
||||
|
||||
RETURN count / times
|
||||
END Averages;
|
||||
|
||||
VAR
|
||||
i: INTEGER;
|
||||
av,an,df: REAL;
|
||||
BEGIN
|
||||
Random.Randomize;
|
||||
Out.String(" Averages Analytical Diff% ");Out.Ln;
|
||||
FOR i := 1 TO 20 DO
|
||||
Out.Int(i,3); Out.String(": ");
|
||||
av := Averages(i);an := Analytical(i);df := Abs(av - an) / an * 100.0;
|
||||
Out.Fixed(av,10,4);Out.Fixed(an,11,4);Out.Fixed(df,10,4);Out.Ln
|
||||
END
|
||||
END AvgLoopLen.
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
parse arg runs tests seed . /*obtain optional arguments from the CL*/
|
||||
if runs =='' | runs =="," then runs = 40 /*Not specified? Then use the default.*/
|
||||
if tests =='' | tests =="," then tests= 1000000 /* " " " " " " */
|
||||
if datatype(seed,'W') then call random ,, seed /*Is integer? For RAND repeatability.*/
|
||||
if datatype(seed, 'W') then call random ,, seed /*Is integer? For RAND repeatability.*/
|
||||
!.=0; !.0=1 /*used for factorial (!) memoization.*/
|
||||
numeric digits 100000 /*be able to calculate 25k! if need be.*/
|
||||
numeric digits max(9, length( !(runs) ) ) /*set the NUMERIC DIGITS for !(runs). */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue