Skip to content

Instantly share code, notes, and snippets.

@velnias75
Created November 5, 2017 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velnias75/53f9530159123c7d04589b7c5b840b4a to your computer and use it in GitHub Desktop.
Save velnias75/53f9530159123c7d04589b7c5b840b4a to your computer and use it in GitHub Desktop.
Binominal coefficient constant template
/*
* binominal coefficient constant template
*
* g++ --std=c++11 -g0 -O3 -s bicot.cpp -o bicot
*
* (c) 2017 by Heiko Schaefer <heiko@rangun.de>
*
*/
#include <iostream>
#include <cstdlib>
#include <cstdint>
#include <type_traits>
namespace bicot {
template<class T,
typename std::integral_constant<T, T(0)>::value_type n,
typename std::integral_constant<T, T(0)>::value_type k>
struct binomial {
static constexpr T value = (binomial<T, n - T(1), k - T(1)>::value + binomial<T, n - T(1), k>::value);
};
template<class T>
struct binomial<T,
typename std::integral_constant<T, T(0)>::value_type(0),
typename std::integral_constant<T, T(0)>::value_type(0)> {
static constexpr T value = T(1);
};
template<class T,
typename std::integral_constant<T, T(0)>::value_type n>
struct binomial<T, n, typename std::integral_constant<T, T(0)>::value_type(0)> {
static constexpr T value = T(1);
};
template<class T, typename std::integral_constant<T, T(0)>::value_type n>
struct binomial<T, n, n> {
static constexpr T value = T(1);
};
}
int main(int, char**) {
std::cout << bicot::binomial<uint32_t, uint32_t(49), uint32_t(6)>::value << std::endl;
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment