Skip to content

Commit 9372b1e

Browse files
committed
docs: udpate doc-strs and add example()
1 parent 154c9f8 commit 9372b1e

File tree

1 file changed

+42
-31
lines changed

1 file changed

+42
-31
lines changed

math/ncr_modulo_p.cpp

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,20 @@ namespace math {
2626
*/
2727
namespace ncr_modulo_p {
2828

29+
/**
30+
* @namespace utils
31+
* @brief this namespace contains the definitions of the functions called from
32+
* the class NCRModuloP
33+
*/
2934
namespace utils {
30-
/** Finds the value of x, y such that a*x + b*y = gcd(a,b)
35+
/**
36+
* @brief finds the values x and y such that a*x + b*y = gcd(a,b)
3137
*
32-
* @params[in] the numbers 'a', 'b' and address of 'x' and 'y' from above
33-
* equation
34-
* @returns the gcd of a and b
38+
* @param[in] a the first input of the gcd
39+
* @param[in] a the second input of the gcd
40+
* @param[out] x the Bézout coefficient of a
41+
* @param[out] y the Bézout coefficient of b
42+
* @return the gcd of a and b
3543
*/
3644
int64_t gcdExtended(const int64_t& a, const int64_t& b, int64_t& x,
3745
int64_t& y) {
@@ -49,10 +57,11 @@ int64_t gcdExtended(const int64_t& a, const int64_t& b, int64_t& x,
4957
return gcd;
5058
}
5159

52-
/** Find modular inverse of a with m i.e. a number x such that (a*x)%m = 1
60+
/** Find modular inverse of a modulo m i.e. a number x such that (a*x)%m = 1
5361
*
54-
* @params[in] the numbers 'a' and 'm' from above equation
55-
* @returns the modular inverse of a
62+
* @param[in] a the number for which the modular inverse is queried
63+
* @param[in] m the modulus
64+
* @return the inverce of a modulo m, if it exists, -1 otherwise
5665
*/
5766
int64_t modInverse(const int64_t& a, const int64_t& m) {
5867
int64_t x = 0, y = 0;
@@ -91,17 +100,18 @@ class NCRModuloP {
91100
}
92101

93102
public:
94-
/** Constructor which precomputes the values of n! % mod from n=0 to size
95-
* and stores them in vector 'fac'
96-
* @params[in] the numbers 'size', 'mod'
103+
/**
104+
* @brief constructs an NCRModuloP object allowing to compute (nCr)%p for
105+
* inputs from 0 to size
97106
*/
98-
NCRModuloP(const int64_t& size, const int64_t& mod)
99-
: p(mod), fac(computeFactorialsMod(size, mod)) {}
107+
NCRModuloP(const int64_t& size, const int64_t& p)
108+
: p(p), fac(computeFactorialsMod(size, p)) {}
100109

101-
/** Find nCr % p
102-
*
103-
* @params[in] the numbers 'n' and 'r'
104-
* @returns the value nCr % p
110+
/**
111+
* @brief computes nCr % p
112+
* @param[in] n the number of objects to be chosen
113+
* @param[in] r the number of objects to choose from
114+
* @return the value nCr % p
105115
*/
106116
int64_t ncr(const int64_t& n, const int64_t& r) const {
107117
// Base cases
@@ -126,13 +136,7 @@ class NCRModuloP {
126136
} // namespace ncr_modulo_p
127137
} // namespace math
128138

129-
/**
130-
* @brief Test implementations
131-
* @param ncrObj object which contains the precomputed factorial values and
132-
* ncr function
133-
* @returns void
134-
*/
135-
static void tests() {
139+
void tests() {
136140
struct TestCase {
137141
const int64_t size;
138142
const int64_t p;
@@ -161,19 +165,26 @@ static void tests() {
161165
}
162166

163167
/**
164-
* @brief Main function
165-
* @returns 0 on exit
168+
* @brief example showing the usage of the NCRModuloP class
166169
*/
167-
int main() {
168-
// populate the fac array
170+
void example() {
169171
const int64_t size = 1e6 + 1;
170172
const int64_t p = 1e9 + 7;
173+
174+
// the ncrObj contains the precomputed values of factorials modulo p for
175+
// values from 0 to size
171176
const auto ncrObj = math::ncr_modulo_p::NCRModuloP(size, p);
172-
// test 6Ci for i=0 to 7
177+
178+
// having the ncrObj we can efficiently query the values of (n C r)%p
179+
// note that time of the computation does not depend on size
173180
for (int i = 0; i <= 7; i++) {
174-
std::cout << 6 << "C" << i << " = " << ncrObj.ncr(6, i) << "\n";
181+
std::cout << 6 << "C" << i << " mod " << p << " = " << ncrObj.ncr(6, i)
182+
<< "\n";
175183
}
176-
tests(); // execute the tests
177-
std::cout << "Assertions passed\n";
184+
}
185+
186+
int main() {
187+
tests();
188+
example();
178189
return 0;
179190
}

0 commit comments

Comments
 (0)