原题:
If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing by d, i.e., a, a + d, a + 2d, a + 3d, a + 4d, ..., contains infinitely many prime numbers. This fact is known as Dirichlet's Theorem on Arithmetic Progressions, which had been conjectured by Johann Carl Friedrich Gauss (1777 - 1855) and was proved by Johann Peter Gustav Lejeune Dirichlet (1805 - 1859) in 1837.
For example, the arithmetic sequence beginning with 2 and increasing by 3, i.e.,
2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 62, 65, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, ... ,
contains infinitely many prime numbers
2, 5, 11, 17, 23, 29, 41, 47, 53, 59, 71, 83, 89, ... .
Your mission, should you decide to accept it, is to write a program to find thenth prime number in this arithmetic sequence for given positive integers a, d, and n.
题意:
输入两个数a,d,然后由这两个数组成的数列a+d,a+2d,a+3d......中一定包含有无穷多个素数,并不知道为什么(高斯大佬牛逼),然后要我们找出这个数列中第n个素数。
题解:
没啥好办法,打表呗,依次穷举,要是是素数就加到素数表里边,直到得到想要的第n个素数。
代码:AC
#include<iostream>
#include<cmath>
using namespace std;
bool is_prime(int n)
{
if(n==1)
return false;
if(n==2)
return true;
int m;
for(m=2;m<(int)sqrt((double)n)+1;m++)
{
if(n%m==0)
break;
}
if(m==(int)sqrt((double)n)+1)
return true;
else
return false;
}
int data[5000];
int prime[5000];
int main()
{
int a,d,n;
while(cin>>a>>d>>n)
{
if(a+d+n==0)
break;
else
{
int i=0,j=1;
while(j<=n)
{
data[i]=a+d*i;
if(is_prime(data[i]))
{
prime[j]=data[i];
j++;
}
i++;
}
cout<<prime[n]<<endl;
}
}
return 0;
}