Skip to content

Instantly share code, notes, and snippets.

@Astro36
Created April 2, 2020 09:25
Show Gist options
  • Save Astro36/a045545380c3d4be321d394a2f791ca4 to your computer and use it in GitHub Desktop.
Save Astro36/a045545380c3d4be321d394a2f791ca4 to your computer and use it in GitHub Desktop.
#include <iostream>
template<unsigned int N>
constexpr unsigned int factorial_v = N * factorial_v<N - 1>;
template<>
constexpr unsigned int factorial_v<1> = 1;
template<unsigned int N, unsigned int K>
constexpr unsigned int binomial_v = factorial_v<N> / (factorial_v<K> * factorial_v<N - K>);
template<unsigned int... Ks>
constexpr unsigned int multinomial_v = factorial_v<(Ks + ...)> / (factorial_v<Ks> * ...);
int main() {
std::cout << factorial_v<5> << std::endl; // 120
std::cout << binomial_v<5, 2> << std::endl; // 10
std::cout << multinomial_v<5> << std::endl; // 1
std::cout << multinomial_v<3, 2> << std::endl; // 10
std::cout << multinomial_v<2, 2, 2> << std::endl; // 90
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment