Arithmetic prime number sequence-the eighth Lanqiao Cup Group B (provincial competition)

2,3,5,7,11,13,...is a sequence of prime numbers. Similar: 7,37,67,97,127,157 This arithmetic sequence composed entirely of prime numbers is called arithmetic prime sequence.
The tolerance of the sequence above is 30 and the length is 6. In 2004, Green cooperated with Chinese Tao Zhexuan to prove that there is an arithmetic sequence of prime numbers of any length. This is an amazing achievement in the field of number theory!
What is the minimum tolerance of a prime number sequence of length 10?

Note: What needs to be submitted is an integer, do not fill in any redundant content and explanatory text.
Blue Bridge Cup

At the beginning, I wondered if there is a clever way to solve the problem, but I found that sometimes simple methods often save time, so this problem is simply enumerated.
code show as below:

#include <iostream>
#include <string.h>
using namespace std;
#define maxn 100000
int a[maxn];
void prim()
{
    
    
 int i,j;
 for(i=2;i<=maxn;i++)
 {
    
    
  for(j=2;j<=maxn;j++)
  {
    
    
   if(i*j>maxn)
   break;
   a[i*j]=1;
  }
 }
}
int main()
{
    
    
 int i,k,cha,s;
 memset(a,0,sizeof(a));
 a[0]=1;
 a[1]=1;
 prim();
 for(cha=2;cha<=maxn/10;cha++)
 {
    
    
  for(i=2;i<=maxn;i++)
  {
    
    
   if(a[i]==1)
   continue;
   for(k=0;k<=9;k++)
   {
    
    
    s=i+(k*cha);
    if(s>maxn)  //这里开始写代码的时候没注意S的最大取值(数组a最大只有maxn)
    break;
    if(a[s]==1)
    break;
   }
   if(k==10)
   {
    
    
    cout<<cha<<endl;   // cout<<i<<endl;
    return 0;
   }
  }
 }
 return 0;
}

Guess you like

Origin blog.csdn.net/HT24k/article/details/105657745