mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-11-29 23:45:34 +08:00
feat: Add ncr mod p code (#1325)
* feat: Add ncr mod p code (#1323) * Update math/ncr_modulo_p.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Added all functions inside a class + added more asserts * updating DIRECTORY.md * clang-format and clang-tidy fixes forf6df24a5* Replace int64_t to uint64_t + add namespace + detailed documentation * clang-format and clang-tidy fixes fore09a0579* Add extra namespace + add const& in function arguments * clang-format and clang-tidy fixes for8111f881* Update ncr_modulo_p.cpp * clang-format and clang-tidy fixes for2ad2f721* Update math/ncr_modulo_p.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update math/ncr_modulo_p.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update math/ncr_modulo_p.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * clang-format and clang-tidy fixes for5b69ba5c* updating DIRECTORY.md * clang-format and clang-tidy fixes fora8401d4bCo-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief [Combinations](https://en.wikipedia.org/wiki/Combination) n choose r function implementation
|
||||
* @brief [Combinations](https://en.wikipedia.org/wiki/Combination) n choose r
|
||||
* function implementation
|
||||
* @details
|
||||
* A very basic and efficient method of calculating
|
||||
* choosing r from n different choices.
|
||||
@@ -9,8 +10,8 @@
|
||||
* @author [Tajmeet Singh](https://github.com/tjgurwara99)
|
||||
*/
|
||||
|
||||
#include <iostream> /// for io operations
|
||||
#include <cassert> /// for assert
|
||||
#include <iostream> /// for io operations
|
||||
|
||||
/**
|
||||
* @namespace math
|
||||
@@ -22,8 +23,9 @@ namespace math {
|
||||
* @details
|
||||
* We are calculating the ans with iterations
|
||||
* instead of calculating three different factorials.
|
||||
* Also, we are using the fact that
|
||||
* \f$ \frac{n!}{r! (n-r)!} = \frac{(n - r + 1) \times \cdots \times n}{1 \times \cdots \times r} \f$
|
||||
* Also, we are using the fact that
|
||||
* \f$ \frac{n!}{r! (n-r)!} = \frac{(n - r + 1) \times \cdots \times n}{1 \times
|
||||
* \cdots \times r} \f$
|
||||
* @tparam T Only for integer types such as long, int_64 etc
|
||||
* @param n \f$ n \f$ in \f$ \binom{n}{r} \f$
|
||||
* @param r \f$ r \f$ in \f$ \binom{n}{r} \f$
|
||||
@@ -31,16 +33,17 @@ namespace math {
|
||||
*/
|
||||
template <class T>
|
||||
T n_choose_r(T n, T r) {
|
||||
if(r > n / 2)
|
||||
r = n - r; // Because of the fact that nCr(n, r) == nCr(n, n - r)
|
||||
if (r > n / 2) {
|
||||
r = n - r; // Because of the fact that nCr(n, r) == nCr(n, n - r)
|
||||
}
|
||||
T ans = 1;
|
||||
for(int i = 1; i <= r; i++) {
|
||||
for (int i = 1; i <= r; i++) {
|
||||
ans *= n - r + i;
|
||||
ans /= i;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
} // namespace math
|
||||
} // namespace math
|
||||
|
||||
/**
|
||||
* @brief Test implementations
|
||||
@@ -48,25 +51,24 @@ T n_choose_r(T n, T r) {
|
||||
*/
|
||||
static void test() {
|
||||
// First test on 5 choose 2
|
||||
uint8_t t = math::n_choose_r(5, 2);
|
||||
assert(((void)"10 is the answer but function says otherwise.\n",
|
||||
t == 10));
|
||||
std::cout << "First test passes." << std::endl;
|
||||
// Second test on 5 choose 3
|
||||
t = math::n_choose_r(5, 3);
|
||||
assert(((void)"10 is the answer but the function says otherwise.\n",
|
||||
t == 10));
|
||||
std::cout << "Second test passes." << std::endl;
|
||||
// Third test on 3 choose 2
|
||||
t = math::n_choose_r(3, 2);
|
||||
assert(((void)"3 is the answer but the function says otherwise.\n",
|
||||
t == 3));
|
||||
std::cout << "Third test passes." << std::endl;
|
||||
// Fourth test on 10 choose 4
|
||||
t = math::n_choose_r(10, 4);
|
||||
assert(((void)"210 is the answer but the function says otherwise.\n",
|
||||
t == 210));
|
||||
std::cout << "Fourth test passes." << std::endl;
|
||||
uint8_t t = math::n_choose_r(5, 2);
|
||||
assert(((void)"10 is the answer but function says otherwise.\n", t == 10));
|
||||
std::cout << "First test passes." << std::endl;
|
||||
// Second test on 5 choose 3
|
||||
t = math::n_choose_r(5, 3);
|
||||
assert(
|
||||
((void)"10 is the answer but the function says otherwise.\n", t == 10));
|
||||
std::cout << "Second test passes." << std::endl;
|
||||
// Third test on 3 choose 2
|
||||
t = math::n_choose_r(3, 2);
|
||||
assert(
|
||||
((void)"3 is the answer but the function says otherwise.\n", t == 3));
|
||||
std::cout << "Third test passes." << std::endl;
|
||||
// Fourth test on 10 choose 4
|
||||
t = math::n_choose_r(10, 4);
|
||||
assert(((void)"210 is the answer but the function says otherwise.\n",
|
||||
t == 210));
|
||||
std::cout << "Fourth test passes." << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,6 +78,6 @@ static void test() {
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main(int argc, char *argv[]) {
|
||||
test(); // executing tests
|
||||
test(); // executing tests
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user