Skip to content

Instantly share code, notes, and snippets.

@benjamn
Created December 30, 2008 00:34
Show Gist options
  • Save benjamn/41454 to your computer and use it in GitHub Desktop.
Save benjamn/41454 to your computer and use it in GitHub Desktop.
#include <iostream>
// Recursive case:
template <int N> struct Fib {
enum { val = Fib<N-1>::val
+ Fib<N-2>::val };
};
// Base cases:
template <> struct Fib<0> { enum { val = 0 }; };
template <> struct Fib<1> { enum { val = 1 }; };
int main()
{
std::cout << "first ten fibonacci numbers: "
<< Fib<0>::val << ", "
<< Fib<1>::val << ", "
<< Fib<2>::val << ", "
<< Fib<3>::val << ", "
<< Fib<4>::val << ", "
<< Fib<5>::val << ", "
<< Fib<6>::val << ", "
<< Fib<7>::val << ", "
<< Fib<8>::val << ", "
<< Fib<9>::val << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment