/* Compile time Fibonacci calculation. Inspired by
http://e...content-available-to-author-only...2.com/title/C%252B%252B%253A+computing+Fibonacci+numbers+at+compile+time */
typedef long long unsigned int Number;
template<Number N> struct Fibonacci
{
static const Number Value = Fibonacci<N-1>::Value + Fibonacci<N-2>::Value ;
};
template<>
struct Fibonacci<0>
{
static const Number Value = 0;
};
template<>
struct Fibonacci <1>
{
static const Number Value = 1;
};
#include <iostream>
int main()
{
// note: for integers maximum value is: 2147483647
// but here it's long long unsigned which means our maximum will be just short of Fibonacci 94
// the Fibonacci numbers can be verified here
// http://w...content-available-to-author-only...c.uk/hosted-sites/R.Knott/Fibonacci/fibtable.html
std::cout << "Fibonacci (84) = " << Fibonacci<84>::Value<< std::endl;
std::cout << "Fibonacci (92) = " << Fibonacci<92>::Value<< std::endl;
std::cout << "Fibonacci (93) = " << Fibonacci<93>::Value<< std::endl;
std::cout << "Fibonacci (94) = " << Fibonacci<94>::Value<< "/* error long long unsigned overflow *\ " << std::endl;
std::cout << "\n\nMax value LLU: \t " << Number(0)-1 << std::endl;
std::cout <<"Fibonacci 94\t 19740274219868223167 \naccording to www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibtable.html" ;
return 0;
}