
    #include <vector>
    #include <iostream>

    std::vector<unsigned int> pascal(unsigned int n)
    {
        // This variable is static, to cache results.
        // Not a problem, as long as mathematics do not change between two calls,
        // which is unlikely to happen, hopefully.
        static std::vector<std::vector<unsigned int> > triangle;

        if(triangle.size() <= n)
        {
            // Compute for i = last to n-1
            while(triangle.size() != n)
                pascal(triangle.size());

            // Compute for n
            if(n == 0)
                triangle.push_back(std::vector<unsigned int>(1,1));
            else
            {
                std::vector<unsigned int> result(n + 1, 0);
                for(unsigned int k = 0; k <= n; k++)
                {
                    unsigned int l = (k > 0 ? triangle[n - 1][k - 1] : 0);
                    unsigned int r = (k < n ? triangle[n - 1][k] : 0);
                    result[k] = l + r;
                }
                triangle.push_back(result);
            }
        }

        // Finish
        return triangle[n];
    }

    std::ostream& operator<<(std::ostream& out, const std::vector<unsigned int>& v)
    {
        for(unsigned int t = 0; t < v.size(); t++)
            out << v[t] << " ";
        return out;
    }

    int main()
    {
        for(unsigned int t = 0; t < 6; t++)
            std::cout << pascal(t) << "\n";
    }

