Average loop length: Difference between revisions

(Undo revision 334659 by Markus65 (talk))
Tag: Undo
Line 399:
Partial translation of C using stl and std.
<syntaxhighlight lang="cpp">#include <random>
#include <random>
#include <vector>
#include <iostream>
Line 414 ⟶ 415:
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 463 ⟶ 464:
{{out}}
<pre>
n avg exp. diff
-------------------------------
1 1.0000 1.0000 0.000002%
2 1.49984999 1.5000 -0.016006%
3 1.88838897 1.8889 - 0.032042%
4 2.21882177 2.2188 -0.000046%
5 2.51055109 2.5104 0.004018%
6 2.77607768 2.7747 0.047077%
7 3.01800187 3.0181 - 0.004019%
8 3.2448 3.2450 -0.007008%
9 3.45804600 3.4583 - 0.010049%
10 3.66146619 3.6602 0.032046%
11 3.85328526 3.8524 0.022006%
12 4.03490391 4.0361 - 0.029076%
13 4.21532129 4.2123 0.070012%
14 4.38193858 4.3820 - 0.003087%
15 4.54945469 4.5458 0.079023%
16 4.70827045 4.7043 0.084006%
17 4.85768587 4.8579 - 0.005016%
18 5.00280071 5.0071 - 0.084001%
19 5.14841529 5.1522 - 0.073013%
20 5.29392931 5.2936 -0.006010%
 
</pre>
3

edits