Average loop length: Difference between revisions

Undo revision 334659 by Markus65 (talk)
(Undo revision 334659 by Markus65 (talk))
Tag: Undo
Line 396:
</pre>
 
=={{header|C++}}==
#include <random>
Partial translation of C using stl and std.
<syntaxhighlight lang="cpp">#include <random>
#include <vector>
#include <iostream>
Line 412 ⟶ 414:
int randint(int n) {
int r, rmax = RAND_MAX / n * n;
dis=std::uniform_int_distribution<int>(0,rmax) ;
r = dis(gen);
return r / (RAND_MAX / n);
}
 
unsigned long long factorial(size_t n) {
//Factorial using dynamic programming to memoize the values.
static std::vector<unsigned long long>factorials{1,1,2};
for (;factorials.size() <= n;)
factorials.push_back(((unsigned long long) factorials.back())*factorials.size());
return factorials[n];
}
Line 458 ⟶ 460:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
n avg exp. diff
-------------------------------
1 1.0000 1.0000 0.000%
2 1.4998 1.5000 -0.016%
3 1.8883 1.8889 -0.032%
4 2.2188 2.2188 0.000%
5 2.5105 2.5104 0.004%
6 2.7760 2.7747 0.047%
7 3.0180 3.0181 -0.004%
8 3.2448 3.2450 -0.007%
9 3.4580 3.4583 -0.010%
10 3.6614 3.6602 0.032%
11 3.8532 3.8524 0.022%
12 4.0349 4.0361 -0.029%
13 4.2153 4.2123 0.070%
14 4.3819 4.3820 -0.003%
15 4.5494 4.5458 0.079%
16 4.7082 4.7043 0.084%
17 4.8576 4.8579 -0.005%
18 5.0028 5.0071 -0.084%
19 5.1484 5.1522 -0.073%
20 5.2939 5.2936 0.006%
 
</pre>
 
=={{header|Clojure}}==
3

edits