#include <iostream> #include <string> using namespace std; //from https://stackoverflow.com/questions/1801391/what-is-the-best-algorithm-for-checking-if-a-number-is-prime bool isPrime(long long int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n%2 == 0 || n%3 == 0) return false; for (long long int i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } int main() { bool flag = false; long long int number; cin >> number; string string_number = to_string(number); string first_number; string second_number; for(int i = 1; i< string_number.size(); i++) { first_number = string_number.substr(0,i); second_number = string_number.substr(i, string_number.size()); if(second_number.find("0") == 0) { continue; } long long int number1 = stoll(first_number); long long int number2 = stoll(second_number); if(isPrime(number1) && isPrime(number2)) { cout << "TAK" << endl; flag = true; } } if(!flag) { cout << "NIE" << endl; } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #include <iostream> #include <string> using namespace std; //from https://stackoverflow.com/questions/1801391/what-is-the-best-algorithm-for-checking-if-a-number-is-prime bool isPrime(long long int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n%2 == 0 || n%3 == 0) return false; for (long long int i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } int main() { bool flag = false; long long int number; cin >> number; string string_number = to_string(number); string first_number; string second_number; for(int i = 1; i< string_number.size(); i++) { first_number = string_number.substr(0,i); second_number = string_number.substr(i, string_number.size()); if(second_number.find("0") == 0) { continue; } long long int number1 = stoll(first_number); long long int number2 = stoll(second_number); if(isPrime(number1) && isPrime(number2)) { cout << "TAK" << endl; flag = true; } } if(!flag) { cout << "NIE" << endl; } return 0; } |