[ create a new paste ] login | about

Link: http://codepad.org/vXqhYRaq    [ raw code | output | fork ]

C++, pasted on Dec 10:
#include <iostream>         // std::wcout, std::endl
#include <vector>
using namespace std;

class Fibonacci
{
    vector<int> memo;

    int fib( int const n )
    {
        if( int( memo.size() ) > n ) { return memo[n]; }

        int const result = fib( n - 1 ) + fib( n - 2 );
        memo.push_back( result );
        return result;
    };

public:
    int operator()( int const n ) { return fib( n ); }

    Fibonacci()
    {
        memo.push_back( 0 );  memo.push_back( 1 );
    }
} fibonacci;

int main()
{
    // 0, 1, 1, 2, 3, 5, 8
    cout << fibonacci( 6 ) << endl;
}


Output:
1
8


Create a new paste based on this one


Comments: