martes, 10 de abril de 2012

Project Euler 44

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
bool esPentagonal(int x)
{
    double n = (sqrt(24.0*x + 1)+1)/6.0;
    return n==floor(n);
}
int obtPent(int n)
{
    return (n*(3*n-1))/2;
}
int main()
{
    vector<int> pentagonals;
    pentagonals.push_back(1);
    bool sw = true;
    int pent,resta;
    int k = 2;
    while(sw)
    {
        pent = obtPent(k);
        vector<int>::iterator it = pentagonals.begin();
        while(it != pentagonals.end())
        {
            resta = pent - (*it);
            if(esPentagonal(resta))
                if(esPentagonal(pent+(*it)))
                {
                    cout<<resta<<endl;
                    return 0;
                }
            it++;
        }
        pentagonals.push_back(pent);
        k++;
    }
    return 0;
}

No hay comentarios:

Publicar un comentario