/* The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another. There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence. What 12-digit number do you form by concatenating the three terms in this sequence? */ #include <iostream> #include <vector> #include <cmath> using namespace std; const int size = 10000; bool prime[size] = {false}; vector<int> primes; bool IsPrime(long long int n) { if(n%2==0) return false; long long int hd=8; long long int lg=6; long long int lh=8; long long int va=n-1; while(va>=hd) { if(!((va-hd)%lg)) return false; lh+=8; lg+=4; hd+=lh; } return true; } bool isPrime(int n) { if (n==1) return false; for(int i=2; i * i <= n; i+=1) { if (n % i == 0) return false; } return true; } bool problem49(int N) { int temp = N; int d = temp % 10; temp /= 10; int c = temp % 10; temp /= 10; int b = temp % 10; temp /= 10; int a = temp % 10; int NN = b*1000+c*100+a*10+d; int NNN = c*1000+a*100+b*10+d; if (N==NN || N==NNN) return false; if (d % 2 == 0) return false; if (!prime[NN]) return false; if (!prime[NNN]) return false; if (NNN - NN == NN - N) return true; return false; } int main() { for (int i=1000; i<10000; i++) if (IsPrime(i)) { primes.push_back(i); prime[i] = true; } for (size_t i=0; i<primes.size(); i++) if ( problem49(primes[i]) ) cout << primes[i] << endl; }