miércoles, 11 de abril de 2012

Project Euler 47

#include <iostream>
#include <vector>
#include <set>
using namespace std;

bool Factorizar(int n,int nrofactores,vector<int>* factores)
{
    int i;
    int cant = 0;
    for(i = 2; n>1 ; i++)
        if((n%i) == 0 )
        {
 
            cant++;
            if(cant>nrofactores)
                return false;
            int factor =i;
            n/=i;
            while((n%i) == 0)
                n/=i ,factor*=i;
            factores->push_back(factor);
        }
    return (cant==nrofactores);
}
int main()
{
    int ini = 1;
    vector<int> a,b,c,d;
    bool swa,swb,swc,swd;
    swa =swb=swc=swd = false;
    while(true)
    {
        swa = swb;
        swb = swc;
        swc = swd;
        a.swap(b);
        b.swap(c);
        c.swap(d);
        d.clear();
        swd = Factorizar(ini,4,&d);
        //cout <<ini<<" = " << swa <<" "<< swb <<" "<< swc <<" "<< swd <<endl;
        if(swa && swb && swc && swd)
        {
            set<int> miset(a.begin(),a.end());
            miset.insert(b.begin(),b.end());
            miset.insert(c.begin(),c.end());
            miset.insert(d.begin(),d.end());
            if(miset.size()==16)
            {
                cout << ini<<endl;
                return 0;
            }
        }
        ini++;
    }
    return 0;
}

No hay comentarios:

Publicar un comentario