
#include <iostream>
#include <time.h>
__int64 test(__int64 start, __int64 end)
{
__int64 total = 0;
if((end - start) % 2 == 0)
{
total = ((start+end)*((end-start)/2)) + (end/2) + 1;
}
else
{
total = (end + start) * (end/2);
}
return total;
}
__int64 test2(__int64 start, __int64 end)
{
__int64 total = 0;
for(__int64 i = 1; i <= end; i++)
{
total += i;
}
return total;
}
int main()
{
int sum = 0;
clock_t start, end,start2,end2;
start = clock();
for(int i = 0; i < 10000; i++)
{
std::cout << test(1,10000000) << '\n';
}
end = clock();
double time1 = (double)(end-start)/CLOCKS_PER_SEC;
std::cout << "Time required: " << time1 << " seconds.\n";
std::cout << "For the for loop function. Press enter. . . \n";
std::cin.get();
start2 = clock();
for(int i = 0; i < 10000; i++)
{
std::cout << test2(1,10000000) << '\n';
}
end2 = clock();
double time2 = (double)(end2-start2)/CLOCKS_PER_SEC;
std::cout << "Time required: " << time2 << " seconds.\n";
std::cout << "Formula executed " << time2 - time1 << " seconds faster than traditional for loop solution\n";
return 0;
}


__int64 test3(__int64 start, __int64 end)
{
__int64 n = end - start + 1;
return n*(start + end)/2;
}
template<int N, int S=1, int D=1>
struct sequence
{
enum { sum = (N >> 1) * ((S << 1) + (N - 1) * D) };
};
N = number of elements in the sequence S = starting number of the sequence D = difference between each term
sequence<100>::sum; // every term sequence<100,1,3>::sum; // every 3rd term ..etc