justinpuckett

Untitled

May 26th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6.  
  7. bool isPrime(int number)
  8. {
  9.     for (int divisor = 2; divisor <= number / 2; divisor++)
  10.     {
  11.         if (number % divisor == 0)
  12.         {
  13.             return false;
  14.         }
  15.     }
  16.     return true;
  17. }
  18. //-----------------------------------------------------------------------------------------------------------------------
  19. int PrimeNumber(int number)
  20. {
  21.     if (isPrime(number))
  22.     {
  23.     return number;
  24.     }
  25. }
  26. //---------------------------------------------------------------------------------------------------------------------------------
  27. char digitToChar(int d)
  28. {
  29.     return static_cast<char>(d + '0');
  30. }
  31. //---------------------------------------------------------------------------------------------------------------------------------------------
  32. string intToString(int number)
  33. {
  34.     string s = "";
  35.  
  36.     while (PrimeNumber(number) > 0)
  37.     {
  38.         s = digitToChar(number % 10) + s;
  39.         number = number / 10;
  40.     }
  41.     if (s == "")
  42.         s = digitToChar(0);
  43.  
  44.     return s;
  45. }
  46. //-----------------------------------------------------------------------------------------------------------------------------------------------
  47. void printPALS(int numberOfPalendromes)
  48. {
  49.     bool isPalendrome = true;
  50.     {
  51.         const int NUMBER_OF_PALS_PER_LINE = 10;
  52.         int count = 0;
  53.         int number = 2;
  54.  
  55.  
  56.         while (count < numberOfPalendromes)
  57.         {
  58.             //Plugs number into string function, which checks for prime before sending back
  59.             string pal = intToString(number);
  60.             int low = 0;
  61.             int high = pal.length() - 1;
  62.  
  63.             //Checks for palendrome. If it isn't, breaks and checks next prime.
  64.             while (low < high)
  65.             {
  66.                 if (pal[low] != pal[high])
  67.                 {
  68.                     isPalendrome = false;
  69.                     break;
  70.                 }
  71.                 low++;
  72.                 high--;
  73.             }
  74.             //If it is a palendrome, starts putting them in columns until 100
  75.             if (isPalendrome)
  76.             {
  77.                 count++;
  78.                 if (count == NUMBER_OF_PALS_PER_LINE)
  79.                 {
  80.                     cout << setw(4) << number << endl;
  81.                 }
  82.                 else
  83.                 {
  84.                     cout << setw(4) << number;
  85.                 }
  86.                 number++;
  87.             }
  88.         }
  89.     }
  90. }
  91. int main()
  92. {
  93.     cout << "The first 100 Prime Pal numbers are \n";
  94.     printPALS(100);
  95.  
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment