Average loop length: Difference between revisions
→{{header|C++}}
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
-------------------------------
1 1.0000 1.0000 0.
2 1.
3 1.
4 2.
5 2.
6 2.
7 3.
8 3.2448 3.2450 -0.
9 3.
10 3.
11 3.
12 4.
13 4.
14 4.
15 4.
16 4.
17 4.
18 5.
19 5.
20 5.
</pre>
|