TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
sum_of_binomial_coefficient.cpp File Reference

Algorithm to find sum of binomial coefficients of a given positive integer. More...

#include <cassert>
#include <cstdint>
#include <iostream>
Include dependency graph for sum_of_binomial_coefficient.cpp:

Go to the source code of this file.

Namespaces

namespace  math
 for assert
 

Functions

uint64_t math::binomialCoeffSum (uint64_t n)
 
static void test ()
 
int main ()
 Main function.
 

Detailed Description

Algorithm to find sum of binomial coefficients of a given positive integer.

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 also
more on https://en.wikipedia.org/wiki/Binomial_coefficient#Sums_of_the_binomial_coefficients
Author
muskan0719

Definition in file sum_of_binomial_coefficient.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 64 of file sum_of_binomial_coefficient.cpp.

64 {
65 test(); // execute the tests
66 return 0;
67}
static void test()

◆ test()

static void test ( )
static

Function for testing binomialCoeffSum function. test cases and assert statement.

Returns
void

Definition at line 38 of file sum_of_binomial_coefficient.cpp.

38 {
39 int test_case_1 = math::binomialCoeffSum(2);
40 assert(test_case_1 == 4);
41 std::cout << "Test_case_1 Passed!" << std::endl;
42
43 int test_case_2 = math::binomialCoeffSum(3);
44 assert(test_case_2 == 8);
45 std::cout << "Test_case_2 Passed!" << std::endl;
46
47 int test_case_3 = math::binomialCoeffSum(4);
48 assert(test_case_3 == 16);
49 std::cout << "Test_case_3 Passed!" << std::endl;
50
51 int test_case_4 = math::binomialCoeffSum(5);
52 assert(test_case_4 == 32);
53 std::cout << "Test_case_4 Passed!" << std::endl;
54
55 int test_case_5 = math::binomialCoeffSum(7);
56 assert(test_case_5 == 128);
57 std::cout << "Test_case_5 Passed!" << std::endl;
58}
uint64_t binomialCoeffSum(uint64_t n)