-
-
Save logarytm/a3bb653ca61e3450b73f7dc74ccdbf39 to your computer and use it in GitHub Desktop.
compile-time Pascal triangle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
typedef long long int cardinal; | |
template<cardinal N, cardinal K> | |
struct binomial_coefficient { | |
static constexpr cardinal value = | |
binomial_coefficient<N - 1, K - 1>::value + binomial_coefficient<N - 1, K>::value; | |
}; | |
template<cardinal N> | |
struct binomial_coefficient<N, N> { | |
static constexpr cardinal value = 1; | |
}; | |
template<cardinal N> | |
struct binomial_coefficient<N, 0> { | |
static constexpr cardinal value = 1; | |
}; | |
template<cardinal N, cardinal K> | |
struct pascal_triangle_row | |
{ | |
pascal_triangle_row() | |
{ | |
std::cout << binomial_coefficient<N, K>::value << " "; | |
pascal_triangle_row<N, K - 1>(); | |
} | |
}; | |
template<cardinal N> | |
struct pascal_triangle_row<N, -1> | |
{ | |
pascal_triangle_row() | |
{ | |
/* do nothing */ | |
} | |
}; | |
template<cardinal Limit> | |
void pascal_triangle() | |
{ | |
pascal_triangle<Limit - 1>(); | |
pascal_triangle_row<Limit, Limit>(); | |
std::cout << std::endl; | |
} | |
template<> | |
void pascal_triangle<0>() | |
{ | |
/* do nothing */ | |
} | |
int main() | |
{ | |
pascal_triangle<6>(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment