martes, 10 de abril de 2012

Project Euler 43

#include <iostream>
#include <algorithm>
#include <stdlib.h>
using namespace std;
int primos[8] = {2,3,5,7,11,13,17};
int main()
{
    string ini = "0123456789";
    string fin = "9876543210";
    unsigned long long int suma = 0 ;
    while(ini.compare(fin)!=0)
    {
        bool sw = true;
        for(int i = 7; i >= 1 && sw; i--)
            sw = ((((ini[i]-'0')*100+(ini[i+1]-'0')*10+(ini[i+2]-'0'))%primos[i-1])==0);
        if(sw)
            suma += atof(ini.data());

        next_permutation(ini.begin(),ini.end());
    }
    cout <<suma << endl;
    return 0;
}

No hay comentarios:

Publicar un comentario