Permutation test: Difference between revisions

Added a C++ version that is somewhat convoluted but showcases some functionalities not present in C
m (→‎{{header|REXX}}: added/changed whitespace and comments.)
(Added a C++ version that is somewhat convoluted but showcases some functionalities not present in C)
Line 254:
100 * le / total, le, 100 * gt / total, gt);
return 0;
}</lang>
Output:<lang><= : 87.197168% 80551
> : 12.802832% 11827</lang>
 
=={{header|C++}}==
<lang cpp>#include<iostream>
#include<vector>
#include<numeric>
#include<functional>
 
class
{
public:
int64_t operator()(int n, int k){ return partial_factorial(n, k) / factorial(n - k);}
private:
int64_t partial_factorial(int from, int to) { return from == to ? 1 : from * partial_factorial(from - 1, to); }
int64_t factorial(int n) { return n == 0 ? 1 : n * factorial(n - 1);}
}combinations;
 
int main()
{
static constexpr int treatment = 9;
const std::vector<int> data{ 85, 88, 75, 66, 25, 29, 83, 39, 97,
68, 41, 10, 49, 16, 65, 32, 92, 28, 98 };
 
int treated = std::accumulate(data.begin(), data.begin() + treatment, 0);
 
std::function<int (int, int, int)> pick;
pick = [&](int n, int from, int accumulated)
{
if(n == 0)
return accumulated > treated ? 1 : 0;
else
return pick(n - 1, from - 1, accumulated + data[from - 1]) +
(from > n ? pick(n, from - 1, accumulated) : 0);
};
 
int total = combinations(data.size(), treatment);
int greater = pick(treatment, data.size(), 0);
int lesser = total - greater;
 
std::cout << "<= : " << 100.0 * lesser / total << "% " << lesser << std::endl
<< " > : " << 100.0 * greater / total << "% " << greater << std::endl;
}</lang>
Output:<lang><= : 87.197168% 80551