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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <cmath>
using namespace std;

string const YES = "TAK";
string const NO = "NIE";
unsigned int primes[78499] = {};
int max_counter = 0;

unsigned long long int remove_begin_digits(unsigned long long int n, unsigned long long int counter = 1) {
    if (n < pow(10, counter)) return 0;
    else return n % 10 + remove_begin_digits(n / 10, counter) * 10;
}

unsigned long long int remove_end_digits(unsigned long long int n, unsigned long long int counter = 1) {
    return n / pow(10, counter);
}

bool is_prime(unsigned int n) {
  if (n <= 1) {
    return false;
  }
  if (n == 2) {
    return true;
  }
  for (int i = 2; i*i <= n; i++) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}

bool is_prime_opt(unsigned long long int n) {
  if (n <= 1) {
    return false;
  }
  if (n == 2) {
    return true;
  }
  for (int i = 0; i < max_counter && primes[i]*primes[i] < n; i++) {
    if (n % primes[i] == 0) {
      return false;
    }
  }
  return true;
}

int main(int argc, char const *argv[]) {
  unsigned long long int input, largest, left, right;
  cin >> input;

  largest = remove_begin_digits(input);
  if (remove_end_digits(input) > largest) {
    largest = remove_end_digits(input);
  }
  largest = sqrt(largest);

  for (unsigned int i = 2; i <= largest; i++) {
    if (is_prime(i)) {
      primes[max_counter++] = i;
    }
  }

  int num_len = to_string(input).length();

  for (int i = 1; i <= num_len - 1; i++) {
    left = remove_end_digits(input, i);
    if (is_prime_opt(left)) {
      // check leading 0 for next number
      left = remove_end_digits(input, i-1);
      if (left % 10 == 0) {
        continue;
      }

      right = remove_begin_digits(input, num_len-i);
      if (is_prime_opt(right)) {
        cout << YES;
        return 0;
      }
    }
  }

  cout << NO;
  return 0;
}