2,3,5,7,11,13,.... 是素数序列。 类似:7,37,67,97,127,1577,37,67,97,127,157 这样完全由素数组成的等差数列,叫等差素数数列。
上边的数列公差为 3030,长度为 66。
20042004 年,格林与华人陶哲轩合作证明了:存在任意长度的素数等差数列。 这是数论领域一项惊人的成果!
有这一理论为基础,请你借助手中的计算机,满怀信心地搜索:
长度为 1010 的等差素数列,其公差最小值是多少?
运行限制
- 最大运行时间:1s
- 最大运行内存: 128M
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int su (int n){
for(int i=2;i<n;i++){
if(n%i==0)
return 0;
}
return 1;
}
int main(int argc, char** argv) {
int count=0;
for(int i=2;i<1000;i++){
if(su(i)){
for(int j=3;j<5000;j++){//枚举首项
for(int a=0;a<10;a++){//枚举公差
if(su(i+a*j)){
count++;
}else{
count=0;
break;
}
if(count==9){
cout<<j<<"\n";
return 0;
}else{
continue;
}
}
}
}else{
continue;
}
}
return 0;
}
还有另一个方法:所有小于等差素数列长度的素数的乘积所得结果就是该等差素数列的公差