From 0f8324ed49ae0a33224a0e14912552c58c2af345 Mon Sep 17 00:00:00 2001 From: muskan0719 <53470969+muskan0719@users.noreply.github.com> Date: Wed, 28 Oct 2020 14:23:57 +0530 Subject: [PATCH] feat: add sum_of_binomial_coefficients (#1363) * Add files via upload * update sum_of_binomial_coefficients * Update math/sum_of_binomial_coefficient.cpp Co-authored-by: David Leal * Update math/sum_of_binomial_coefficient.cpp Co-authored-by: David Leal * updating DIRECTORY.md * clang-format and clang-tidy fixes for e7ac2255 * Update math/sum_of_binomial_coefficient.cpp Co-authored-by: David Leal * Update sum_of_binomial_coefficient.cpp * Update math/sum_of_binomial_coefficient.cpp Co-authored-by: Ayaan Khan Co-authored-by: David Leal Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Ayaan Khan --- DIRECTORY.md | 1 + math/sum_of_binomial_coefficient.cpp | 67 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 math/sum_of_binomial_coefficient.cpp diff --git a/DIRECTORY.md b/DIRECTORY.md index cf6f68050..1bdffd780 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -156,6 +156,7 @@ * [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/sieve_of_eratosthenes.cpp) * [Sqrt Double](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/sqrt_double.cpp) * [String Fibonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/string_fibonacci.cpp) + * [Sum Of Binomial Coefficient](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/sum_of_binomial_coefficient.cpp) * [Sum Of Digits](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/sum_of_digits.cpp) ## Numerical Methods diff --git a/math/sum_of_binomial_coefficient.cpp b/math/sum_of_binomial_coefficient.cpp new file mode 100644 index 000000000..e0dc826b8 --- /dev/null +++ b/math/sum_of_binomial_coefficient.cpp @@ -0,0 +1,67 @@ +/** + * @file + * @brief Algorithm to find sum of binomial coefficients of a given positive integer. + * @details Given a positive integer n, the task is to find the sum of binomial coefficient i.e + * nC0 + nC1 + nC2 + ... + nCn-1 + nCn + * By induction, we can prove that the sum is equal to 2^n + * @see more on https://en.wikipedia.org/wiki/Binomial_coefficient#Sums_of_the_binomial_coefficients + * @author [muskan0719](https://github.com/muskan0719) + */ +#include /// for std::cin and std::cout +#include /// for assert + +/** + * @namespace math + * @brief Mathematical algorithms + */ +namespace math { + + /** + * Function to calculate sum of binomial coefficients + * @param n number + * @return Sum of binomial coefficients of number + */ + uint64_t binomialCoeffSum(uint64_t n) + { + // Calculating 2^n + return (1 << n); + } +} // namespace math + +/** + * Function for testing binomialCoeffSum function. + * test cases and assert statement. + * @returns `void` +*/ +static void test() +{ + int test_case_1 = math::binomialCoeffSum(2); + assert(test_case_1==4); + std::cout<<"Test_case_1 Passed!"<