Project Euler Homework

004.cpp

Go to the documentation of this file.
00001 
00010 #include <iostream>
00011 int
00012 main ()
00013 {
00014   unsigned max_product = 0;
00015   int a = 0, b = 0;
00016   for (int i = 999; i >= 100; i--)
00017     for (int j = 999; j >= 100; j--)
00018       if(i>=j){//Avoid symmetry
00019         unsigned product = i * j;
00020 
00021         if (product < max_product)
00022           continue;             //Exclude smaller products
00023 
00024         if (product >= 1e5)     //six digits
00025           {
00026             if (product / unsigned(1e5) == product % 10
00027                 && product / unsigned(1e4) % 10 == product / 10 % 10
00028                 && product / unsigned(1e3) % 10 == product / 100 % 10)
00029               {
00030                 max_product = product;
00031                 a = i;
00032                 b = j;
00033               }
00034           }
00035         else                    //five digits
00036         if (product / unsigned(1e4) == product % 10
00037               && product / unsigned(1e3) % 10 == product / 10 % 10)
00038           {
00039             max_product = product;
00040             a = i;
00041             b = j;
00042           }
00043       }
00044 
00045 std::cout << a << " * " << b << " = " << max_product << std::endl;
00046   return 0;
00047 }
00048 
 All Classes Files Functions Variables