quote
fund Arithmetic prime sequence
Wake up at night Arithmetic prime sequence
tremble
At first, I didn't understand. I actually wanted to use dfs to list the equal difference sequence one by one. Suddenly, I thought, this is not very realistic. First of all, you don't know where the starting position is. Secondly, you don't know the tolerance, so the arrangement of the first few numbers alone is enough for you to wait. If you are interested in verification, you can try my code. There should be no problem, but you are too lazy to wait~
Later, after reading other people's solutions, I found that I thought the problem was complicated from the beginning. There is no need to find the arrangement, just determine whether the starting position is a prime number, and then traverse from tolerance 2 to find the last nine numbers. From the nature of the question, we can know that although it is a blank filling question, it can certainly run in 1 ~ 2 seconds, so the data range can be set at 1e5 and the tolerance can be set within 1000.
The next step is to judge prime numbers. You can preprocess all prime numbers with algorithms such as linear sieve or Egyptian sieve, or judge them one by one (the large factor in its pair of factors will not be greater than its arithmetic square root), which is also called trial division
Title Description
2, 3, 5, 7, 11, 13,... Are prime sequences. Similar: 7,37,67,97127157 such an arithmetic sequence composed entirely of prime numbers is called arithmetic prime sequence.
The upper series has a tolerance of 30 and a length of 6.
In 2004, green worked with Tao Zhehuan, a Chinese, to prove that there are prime arithmetic sequences of arbitrary length. This is an amazing achievement in the field of number theory!
Based on this theory, please search with confidence with the help of your computer:
What is the minimum tolerance for a 10 length sequence of isometric prime numbers?
Sample
No sample
algorithm
(linear screen) O ( n ) O(n) O(n)
Use the linear sieve to save the prime number in one array and the screened number in another array.
When judging, directly take the value of the starting position as a n = a 1 + ( n − 1 ) k a_n = a_1 + (n - 1)k an = in a1 + (n − 1)k a 1 a_1 a1 , t + j ∗ k t + j * k t+j * k, that is a n a_n An, use it as an index to judge whether the next nine numbers are prime numbers. If all nine numbers meet, then the tolerance at this time is the minimum tolerance.
(trial division to judge prime) O ( n ) O(\sqrt{n}) O(n )
It's the same as judging prime numbers ( n ) \sqrt(n) ( n) times
The practice here is similar to the above. Look at the code directly, with a slight difference
C + + code
// Linear sieve include <iostream> using namespace std; const int N = 1e5 + 10; int primes[N] , cnt; // Primes [] stores all primes bool st[N]; // st[x] is storage x screened out int flag; int i , j , k; void get_primes(int n) { for (int i = 2; i <= n; i ++ ) { if (st[i]) continue; primes[cnt ++ ] = i; for (int j = i + i; j <= n; j += i) st[j] = true; } } int main() { get_primes(100000); for ( i = 0; i < 1000; i ++ ) { int t = primes[i]; for ( j = 2; j < 1000; j ++ ) { for ( k = 1; k < 10; k ++ ) { if (st[t + j * k]) { break; } } if (k == 10) { cout << j; return 0; } } } return 0; }
#include<iostream> #include<cmath> using namespace std; bool isPrimeNumber(int t); int main() { bool flag = true; for (int i = 2; i < 1000; i++) { // i: Starting prime for (int j = 2; j < 1000; j++) { // j: Tolerance of equal difference sequence flag = true; for (int k = 0; k < 10; k++) { // k: The kth prime if (!isPrimeNumber(i + j * k)) { // Judge whether the numbers in the arithmetic sequence are prime numbers flag = false; break; } } if (flag) { cout << j; break; } // Output results } } return 0; } /* ** Judge whether a number is a prime number. If yes, it returns true; otherwise, it returns false */ bool isPrimeNumber(int t) { for (int i = 2; i <= sqrt(t); i++) { if (t % i == 0) return false; } return true; }
// Violence enumeration #include <iostream> using namespace std; const int N = 1e5 + 10; int primes[N], cnt; // Primes [] stores all primes bool st[N]; // st[x] is storage x screened out int n = 10; int perm[N]; int len; // Finding prime number by linear sieve method void get_primes(int n) { for (int i = 2; i <= n; i ++ ) { if (st[i]) continue; primes[cnt ++ ] = i; for (int j = i + i; j <= n; j += i) st[j] = true; } } // dfs permutation enumeration void dfs(int u , int start) { if(u + len - start < n) return; if(u > n) { int tmp = perm[2] - perm[1] , cnt1 = 1; for(int i = 3; i <= n; i ++){ if (perm[i] - perm[i - 1] != tmp) break; else cnt1++; } if (cnt1 == 9) { for (int i = 1; i <= n; i ++) printf("%d", perm[i]); puts(""); } return ; } for(int i = start; i < len; i++) { perm[u] = primes[i]; dfs(u + 1 , i + 1); perm[u] = 0; } } int main() { // Please enter your code here get_primes(100000); len = cnt - 1; dfs(1 , 0); return 0; }