Euler_55.cpp (863B)
1 #include <sstream> 2 3 #include "Euler.h" 4 5 bool isPalindrome(cpp_int i) 6 { 7 std::ostringstream oss; 8 oss << i; 9 10 std::string temp = oss.str(); 11 12 for (unsigned int j = 0; j < temp.length() / 2 + 1; ++j) 13 if (temp.at(j) != temp.at(temp.length() - 1 - j)) 14 return false; 15 16 return true; 17 } 18 19 cpp_int reverse(cpp_int i) 20 { 21 cpp_int reverse = 0; 22 23 while(i > 0) 24 { 25 reverse = reverse * 10 + (i % 10); 26 i /= 10; 27 } 28 29 return reverse; 30 } 31 32 cpp_int Euler::LychrelNumbers() 33 { 34 int lychel = 9999; 35 36 for (int i = 1; i < 10000; ++i) 37 { 38 cpp_int current(i); 39 40 for (int j = 0; j < 50; ++j) 41 { 42 current = current + reverse(current); 43 44 if (isPalindrome(current)) 45 { 46 --lychel; 47 break; 48 } 49 } 50 } 51 52 return lychel; 53 }