Created
November 29, 2017 20:25
-
-
Save ahafidi/0e7b13f80aaedb613cba62cfe9b9bd05 to your computer and use it in GitHub Desktop.
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> | |
template<int n> | |
struct facto | |
{ | |
static constexpr unsigned int value = n * facto<n - 1>::value; | |
}; | |
template<> | |
struct facto<0> | |
{ | |
static constexpr unsigned int value = 1; | |
}; | |
template<int n> | |
struct fibo | |
{ | |
static constexpr unsigned int value = fibo<n - 1>::value | |
+ fibo<n - 2>::value; | |
}; | |
template<> | |
struct fibo<0> | |
{ | |
static constexpr unsigned int value = 0; | |
}; | |
template<> | |
struct fibo<1> | |
{ | |
static constexpr unsigned int value = 1; | |
}; | |
int main() | |
{ | |
std::cout << " 0! = " << facto< 0>::value << "\n" | |
<< " 1! = " << facto< 1>::value << "\n" | |
<< " 2! = " << facto< 2>::value << "\n" | |
<< " 3! = " << facto< 3>::value << "\n" | |
<< " 4! = " << facto< 4>::value << "\n" | |
<< " 5! = " << facto< 5>::value << "\n" | |
<< " 10! = " << facto< 10>::value << "\n" | |
// max value that unsigned int can hold | |
<< " 33! = " << facto< 33>::value << "\n" | |
<< " φ(0) = " << fibo< 0>::value << "\n" | |
<< " φ(1) = " << fibo< 1>::value << "\n" | |
<< " φ(2) = " << fibo< 2>::value << "\n" | |
<< " φ(3) = " << fibo< 3>::value << "\n" | |
<< " φ(4) = " << fibo< 4>::value << "\n" | |
<< " φ(5) = " << fibo< 5>::value << "\n" | |
<< " φ(10) = " << fibo< 10>::value << "\n" | |
// with -ftemplate-depth=1000 | |
<< "φ(1000) = " << fibo<1000>::value << "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment