/* -*- coding: utf-8 -*- * * 843.cc: No.843 Triple Primes - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 500000; /* typedef */ /* global variables */ bool primes[MAX_N + 1]; int pnums[MAX_N]; /* subroutines */ int gen_primes(int maxp, int pnums[]) { memset(primes, true, sizeof(primes)); primes[0] = primes[1] = false; int p, pn = 0; for (p = 2; p * p <= maxp; p++) if (primes[p]) { pnums[pn++] = p; for (int q = p * p; q <= maxp; q += p) primes[q] = false; } for (; p <= maxp; p++) if (primes[p]) pnums[pn++] = p; return pn; } /* main */ int main() { int n; scanf("%d", &n); int pn = gen_primes(n, pnums); //printf("pn=%d\n", pn); int cnt = 0; for (int i = 0; i < pn; i++) { int &pi = pnums[i]; int maxr2 = pi + n; for (int j = 0, r2; (r2 = pnums[j] * pnums[j]) <= maxr2; j++) if (r2 > pi && primes[r2 - pi]) cnt++; } printf("%d\n", cnt); return 0; }