Skip to content

Instantly share code, notes, and snippets.

@renestein
Created August 10, 2012 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renestein/3313956 to your computer and use it in GitHub Desktop.
Save renestein/3313956 to your computer and use it in GitHub Desktop.
FibonaciSumKKataCPP.cpp
// FibonnacciTemplate.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
template<int number>
struct Fibonacci
{
static const __int64 Value = Fibonacci<number - 1>::Value + Fibonacci<number - 2>::Value;
static inline __int64 GetNumber(int i)
{
if (i == number)
{
return Value;
}
else
{
return Fibonacci<number-1>::GetNumber(i);
}
}
};
template<>
struct Fibonacci<0>
{
static const __int64 Value = 1;
static inline __int64 GetNumber(int i)
{
return 1;
}
};
template<>
struct Fibonacci<1>
{
static const __int64 Value = 1;
static inline __int64 GetNumber(int i)
{
if (i == 1)
{
return Value;
}
else
{
return Fibonacci<0>::GetNumber(i);
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
const int FIBONACCI_NUMBERS_LIMIT = 35;
__int64 sum = 0;
for(int i=2; i < FIBONACCI_NUMBERS_LIMIT; i+=3)
{
sum += Fibonacci<FIBONACCI_NUMBERS_LIMIT>::GetNumber(i);
}
cout << sum;
getchar();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment