Project Euler Homework

043.cpp

Go to the documentation of this file.
00001 
00018 #include <iostream>
00019 #include "043_permutation.h"
00020 #include <set>
00021 using namespace std;
00022 
00023 unsigned 
00024 slice_perm (perm043 & perm, idx begin, idx end)
00031 {
00032   unsigned number = 0;
00033   for (idx i = begin; i < end; i++)
00034     number = number * 10 + perm[i];
00035   return number;
00036 }
00037 
00038 unsigned long long
00039 sum (set < unsigned >&numbers)
00040 {
00041   unsigned long long sum = 0;
00042   for (set < unsigned >::iterator p = numbers.begin (); p != numbers.end (); p++)
00043    sum += *p;
00044   return sum;
00045 }
00046 
00047 
00048 int
00049 main ()
00050 {
00051   perm043 perm (10);
00052 
00053   while (perm[0] == 0)
00054     perm.next ();
00055 
00056   set < unsigned >pandigital;
00057   do
00058     {
00059 
00060       if (!(perm[3] & 1) &&     //divisible by 2
00061           (perm[2] + perm[3] + perm[4]) % 3 == 0 &&     //divisible by 3
00062           (perm[5] ==0 || perm[5] == 5)  &&     //divisible by 5 
00063         slice_perm (perm,4, 6+1) % 7 == 0 &&    //divisible by 7
00064         slice_perm (perm,5, 7+1) % 11 == 0 &&   //divisible by 11
00065         slice_perm (perm,6, 8+1) % 13 == 0 &&   //divisible by 13
00066         slice_perm (perm,7, 9+1) % 17 == 0)     //divisible by 17
00067         {
00068           pandigital.insert (slice_perm (perm, 0, 9+1));
00069           //perm.print_perm ();
00070           //cout <<endl;
00071         }
00072     }
00073   while (perm.next ());
00074 
00075 cout << sum(pandigital) << endl;
00076 
00077   return 0;
00078 }
 All Classes Files Functions Variables