project-euler

https://projecteuler.net/
Log | Files | Refs | README

Euler_4.cpp (711B)


      1 #include <algorithm>
      2 #include <sstream>
      3 
      4 #include "Euler.h"
      5 
      6 bool isPalindrome(int i)
      7 {
      8     std::ostringstream oss;
      9     oss << i;
     10 
     11     std::string temp = oss.str();
     12 
     13     for (unsigned int i = 0; i < temp.length(); ++i)
     14         if (temp.at(i) != temp.at(temp.length() - 1 - i))
     15             return false;
     16 
     17     return true;
     18 }
     19 
     20 int Euler::LargestPalindromeFrom3DigitProduct()
     21 {
     22     std::vector<int> products;
     23 
     24     for (int i = 999; i > 99; --i)
     25     {
     26         for (int j = 999; j >= i; --j)
     27         {
     28             if (isPalindrome(i * j)) {
     29                 products.push_back(i * j);
     30                 break;
     31             }
     32         }
     33     }
     34 
     35     std::sort(products.begin(), products.end());
     36 
     37     return products.back();
     38 }