SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <iostream> | |
| 2 | #include <iomanip> | |
| 3 | #include <cmath> | |
| 4 | using namespace std; | |
| 5 | - | |
| 5 | + | |
| 6 | bool isPrime(int number) | |
| 7 | {
| |
| 8 | - | for (int divisor = 2; divisor <= number / 2; divisor++) |
| 8 | + | for (int divisor = 2; divisor <= number / 2; divisor++) |
| 9 | - | {
|
| 9 | + | {
|
| 10 | - | if (number % divisor == 0) |
| 10 | + | if (number % divisor == 0) |
| 11 | - | {
|
| 11 | + | {
|
| 12 | - | return false; |
| 12 | + | return false; |
| 13 | - | } |
| 13 | + | } |
| 14 | - | } |
| 14 | + | } |
| 15 | - | return true; |
| 15 | + | return true; |
| 16 | } | |
| 17 | int intLen(int number) | |
| 18 | {
| |
| 19 | - | int len = 0; |
| 19 | + | int len = 0; |
| 20 | - | int temp = number; |
| 20 | + | int temp = number; |
| 21 | - | while (temp > 0) |
| 21 | + | while (temp > 0) |
| 22 | - | {
|
| 22 | + | {
|
| 23 | - | temp = temp / 10; |
| 23 | + | temp = temp / 10; |
| 24 | - | len++; |
| 24 | + | len++; |
| 25 | - | } |
| 25 | + | } |
| 26 | - | if (number == 0) |
| 26 | + | if (number == 0) |
| 27 | - | len = 1; |
| 27 | + | len = 1; |
| 28 | - | |
| 28 | + | |
| 29 | - | return len; |
| 29 | + | return len; |
| 30 | } | |
| 31 | - | |
| 31 | + | |
| 32 | bool isPalindrome(int number) | |
| 33 | {
| |
| 34 | - | int length = intLen(number); |
| 34 | + | int length = intLen(number); |
| 35 | - | int n1 = number; |
| 35 | + | |
| 36 | - | |
| 36 | + | for (int i = length; i > 1; i--) |
| 37 | - | for (int i = length; i > 1; i--) |
| 37 | + | {
|
| 38 | - | {
|
| 38 | + | int a = number / (pow(10, i-1)); |
| 39 | - | int a = n1 % 10; |
| 39 | + | int b = number % 10; |
| 40 | - | int n2 = n1 - a; |
| 40 | + | cout << "\nnumber is: " << number << "\ti: " << i << |
| 41 | - | i--; |
| 41 | + | "\ta: " << a << "\tb: " << b << endl; |
| 42 | - | int b = n1 / (pow(10, i)); |
| 42 | + | |
| 43 | - | int n3 = n1 - (b * (pow(10, i))); |
| 43 | + | if (a != b) |
| 44 | - | i--; |
| 44 | + | {
|
| 45 | - | cout << "\nn1 is: " << n1 << " \tn2 is: " << n2 << " \tn3 is: " << n3 << "\ti: " << i << |
| 45 | + | return false; |
| 46 | - | "\ta: " << a << "\tb: " << b << "\tlength: " << length << endl; |
| 46 | + | } |
| 47 | - | |
| 47 | + | number /= 10;cout << "\nnumber is: " << number; |
| 48 | - | if (n2 != n3) |
| 48 | + | number -= b * (pow(10, i-1));cout << "\nnumber is: " << number; |
| 49 | - | {
|
| 49 | + | } |
| 50 | - | return false; |
| 50 | + | return true; |
| 51 | - | break; |
| 51 | + | |
| 52 | - | } |
| 52 | + | |
| 53 | - | } |
| 53 | + | |
| 54 | - | return true; |
| 54 | + | |
| 55 | const int NUMBER_OF_POINTS = 100; | |
| 56 | - | |
| 56 | + | int counter = 0; |
| 57 | int number = 2; | |
| 58 | ||
| 59 | - | const int NUMBER_OF_POINTS = 100; |
| 59 | + | while (counter < numberOfPrimes) |
| 60 | - | int counter = 0; |
| 60 | + | {
|
| 61 | - | int number = 2; |
| 61 | + | if (isPrime(number) && isPalindrome(number)) |
| 62 | - | |
| 62 | + | {
|
| 63 | - | while (counter < numberOfPrimes) |
| 63 | + | counter++; |
| 64 | - | {
|
| 64 | + | if (counter % 10 == 0) |
| 65 | - | if (isPrime(number) && isPalindrome(number)) |
| 65 | + | {
|
| 66 | - | {
|
| 66 | + | cout << setw(7) << number << endl; |
| 67 | - | counter++; |
| 67 | + | } |
| 68 | - | if (counter % 10 == 0) |
| 68 | + | else |
| 69 | - | {
|
| 69 | + | cout << setw(7) << number; |
| 70 | - | cout << setw(4) << number << endl; |
| 70 | + | } |
| 71 | - | } |
| 71 | + | number++; |
| 72 | - | else |
| 72 | + | } |
| 73 | - | cout << setw(4) << number; |
| 73 | + | |
| 74 | - | } |
| 74 | + | |
| 75 | - | number++; |
| 75 | + | int main() |
| 76 | - | } |
| 76 | + | |
| 77 | cout << "First 100 Primes and Palindromes are \n"; | |
| 78 | - | |
| 78 | + | printPrimePalindromes(100); |
| 79 | - | int main() |
| 79 | + | return 0; |
| 80 | - | {
|
| 80 | + | } |