# Algorithms_in_C++: math/binomial_calculate.cpp File Reference

Loading...
Searching...
No Matches
binomial_calculate.cpp File Reference

Program to calculate Binomial coefficients More...

`#include <cassert>`
`#include <cstdint>`
`#include <cstdlib>`
`#include <iostream>`
Include dependency graph for binomial_calculate.cpp:

## Namespaces

namespace | math
| for std::rand
namespace | binomial
| Functions for Binomial coefficients implementation.

## Functions

size_t | math::binomial::calculate (int32_t n, int32_t k)
| Function to calculate binomial coefficients. More...
static void | tests ()
| Test implementations. More...
int | main (int argc, const char *argv[])
| Main function. More...

## Detailed Description

Program to calculate Binomial coefficients

Author
  astronmax

## Function Documentation

## calculate()

size_t math::binomial::calculate | ( | int32_t | n,
|  | int32_t | k
| ) |  |

Function to calculate binomial coefficients.

Parameters

  n | first value
  k | second value

Returns
  binomial coefficient for n and k

{
// basic cases
if (k > (n / 2))
k = n - k;
if (k == 1)
return n;
if (k == 0)
return 1;
size_t result = 1;
for (int32_t i = 1; i <= k; ++i) {
result *= n - k + i;
result /= i;
}
return result;
}
numerical_methods::simpson_method::k
double k(double x)
Another test function.
Definition: composite_simpson_rule.cpp:117
math::fibonacci_sum::result
uint64_t result(uint64_t n)
Definition: fibonacci_sum.cpp:76
Here is the call graph for this function:

## main()

int main | ( | int | argc,
|  | const char * | argv[]
| ) |  |

Main function.

Parameters

  argc | commandline argument count
  argv | commandline array of arguments

Returns
  0 on exit

{
tests(); // run self-test implementations
if (argc < 3) {
std::cout << "Usage ./binomial_calculate {n} {k}" << std::endl;
return 0;
}
int32_t n = atoi(argv[1]);
int32_t k = atoi(argv[2]);
std::cout << math::binomial::calculate(n, k) << std::endl;
return 0;
}
std::atoi
T atoi(T... args)
std::cout
tests
static void tests()
Test implementations.
Definition: binomial_calculate.cpp:56
math::binomial::calculate
size_t calculate(int32_t n, int32_t k)
Function to calculate binomial coefficients.
Definition: binomial_calculate.cpp:32
std::endl
T endl(T... args)
Here is the call graph for this function:

## tests()

static void tests | ( |  | ) |

static

Test implementations.

Returns
  void

{
// tests for calculate function
assert(math::binomial::calculate(1, 1) == 1);
assert(math::binomial::calculate(57, 57) == 1);
assert(math::binomial::calculate(6, 3) == 20);
assert(math::binomial::calculate(10, 5) == 252);
assert(math::binomial::calculate(20, 10) == 184756);
assert(math::binomial::calculate(30, 15) == 155117520);
assert(math::binomial::calculate(40, 20) == 137846528820);
assert(math::binomial::calculate(50, 25) == 126410606437752);
assert(math::binomial::calculate(60, 30) == 118264581564861424);
assert(math::binomial::calculate(62, 31) == 465428353255261088);
std::cout << "[+] Binomial coefficients calculate test completed"
<< std::endl;
}
Here is the call graph for this function: