Created
April 26, 2013 14:04
-
-
Save opavlyshak/5467592 to your computer and use it in GitHub Desktop.
Using C++ templates to compute Fibonacci numbers at compile time
This file contains 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 Fib | |
{ | |
enum { val = Fib<n - 1>::val + Fib<n - 2>::val }; | |
}; | |
template<> struct Fib<0> { enum { val = 0 }; }; | |
template<> struct Fib<1> { enum { val = 1 }; }; | |
int main() | |
{ | |
std::cout << Fib<0>::val << std::endl; | |
std::cout << Fib<1>::val << std::endl; | |
std::cout << Fib<2>::val << std::endl; | |
std::cout << Fib<3>::val << std::endl; | |
std::cout << Fib<4>::val << std::endl; | |
std::cout << Fib<5>::val << std::endl; | |
std::cout << Fib<6>::val << std::endl; | |
getchar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using
OR
you an compute up-to
fib<105>::result
which is17704020980446223138
.