Advertisement
hb20007

Euler4

Dec 1st, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. //A palindromic number reads the same backwards.The largest palindrome made from the product of 2 2 digit nos is 9009 = 91 × 99.
  2. //Find the largest palindrome made from the product of two 3 - digit numbers.
  3.  
  4. #include <iostream>
  5. #include <cmath>
  6. using namespace std;
  7.  
  8. int digitCounter(int x)
  9. {
  10.     int digitCount = 0;
  11.     while (x != 0)
  12.     {
  13.         x /= 10;
  14.         digitCount++;
  15.     }
  16.     return digitCount;
  17. }
  18.  
  19. double digitCountInterpreter(int y)
  20. {
  21.     double digitCountInterpreted = pow(10, (y - 1));
  22.     return digitCountInterpreted;
  23. }
  24.  
  25. double reverser(int z)
  26. {
  27.     int logCounter=1;
  28.     double reverse = 0;
  29.     int fix = z;
  30.     while (z != 0)
  31.     {
  32.         reverse += ((z % 10) * ((digitCountInterpreter(digitCounter(fix)))/logCounter));  
  33.         z /= 10;
  34.         logCounter *= 10;
  35.     }
  36.     return reverse;
  37. }
  38.  
  39. bool palindrome(int num)
  40. {
  41.     if (num == reverser(num)) return true;
  42.         return false;
  43. }
  44.  
  45. bool productOf2ThreeDigitNums(int n)
  46. {
  47.     int int1 = 100, int2 = 100;
  48.     while (int1 < 1000)
  49.     {
  50.         if (n == int1*int2) return true;
  51.         else
  52.             if (int2 != 999) int2++; else
  53.             {
  54.                 int1++;
  55.                 int2 = 100;
  56.             }
  57.     }
  58.     return false;
  59. }
  60.  
  61. int main()
  62. {
  63.     for (int h = 10000; h < 1000000; h++)
  64.         if ((palindrome(h) == true) && (productOf2ThreeDigitNums(h) == true)) cout << h << endl;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement