#include <iostream>
constexpr int factorial(int n) {
return n <= 1 ? 1 : (n * factorial(n - 1));
}
constexpr int fibonacci(unsigned n) {
return n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
std::cout << factorial(5) << '\n';
std::cout << fibonacci(10) << '\n';
}
to:
int main() {
constexpr int a = factorial(12);
constexpr int b = fibonacci(10);
// Check if the values are actually calculated at compile time
static_assert(a == 479001600, "factorial failed\n");
static_assert(b == 55, "fibonacci failed\n");
std::cout << a << '\n';
std::cout << b << '\n';
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.