You are viewing a single comment's thread. Return to all comments →
C++ Solution :
#include <bits/stdc++.h> using namespace std; #define all(v) (v).begin(), (v).end() #define debug(x) cout << #x << " = " << x << endl typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; inline ll Mod(ll x, ll mod) { return x % mod >= 0 ? x % mod : x % mod + mod; } bool *is_prime = new bool[1000001]; void sieve(int N) { for (int i = 0; i <= N; i++) is_prime[i] = 1; is_prime[0] = 0; is_prime[1] = 0; for (int i = 2; i * i <= N; ++i) { if (is_prime[i]) { for (int j = i * i; j <= N; j += i) is_prime[j] = 0; } } } bool is_rotate(int n, int p) { for (int i = 0; i < 7; i++) { int y = n % 10; n /= 10; n += y * p; if (!is_prime[n]) return 0; } return 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); sieve(1000000); ll *ans = new ll[1000001]; memset(ans, 0, sizeof(ans)); int p = 1; for (int i = 2; i < 1000001; i++) { if (i == p * 10) p *= 10; if (is_prime[i] == 0 || is_rotate(i, p) == 0) { ans[i] = ans[i - 1]; continue; } ans[i] = ans[i - 1] + i; } int n; cin >> n; cout << ans[n]; }
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #35: Circular primes
You are viewing a single comment's thread. Return to all comments →
C++ Solution :