Skip to content

Instantly share code, notes, and snippets.

@Santarh
Created May 28, 2012 02:07
Show Gist options
  • Save Santarh/2816840 to your computer and use it in GitHub Desktop.
Save Santarh/2816840 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <functional>
#include <tuple>
std::tuple<unsigned long, unsigned long> fib2( unsigned long n )
{
if ( n == 0 )
{
return std::tuple<unsigned long, unsigned long>( 0, 1 );
}
else
{
std::tuple<unsigned long, unsigned long> ret = fib2( n - 1 );
unsigned long a = std::get<0>( ret );
unsigned long b = std::get<1>( ret );
return std::tuple<unsigned long, unsigned long>( b, a + b );
}
}
unsigned long fib( unsigned long n )
{
return std::get<0>( fib2(n) );
}
int main()
{
for ( size_t i = 1; i <= 10; ++i )
{
std::cout << fib(i) << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment