-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
sum_of_binomial_coefficient.cpp
67 lines (59 loc) · 1.83 KB
/
sum_of_binomial_coefficient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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 <cassert> /// for assert
#include <cstdint>
#include <iostream> /// for std::cin and std::cout
/**
* @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!" << std::endl;
int test_case_2 = math::binomialCoeffSum(3);
assert(test_case_2 == 8);
std::cout << "Test_case_2 Passed!" << std::endl;
int test_case_3 = math::binomialCoeffSum(4);
assert(test_case_3 == 16);
std::cout << "Test_case_3 Passed!" << std::endl;
int test_case_4 = math::binomialCoeffSum(5);
assert(test_case_4 == 32);
std::cout << "Test_case_4 Passed!" << std::endl;
int test_case_5 = math::binomialCoeffSum(7);
assert(test_case_5 == 128);
std::cout << "Test_case_5 Passed!" << std::endl;
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // execute the tests
return 0;
}