一、热身 [Cloned] O - Dirichlet's Theorem on Arithmetic Progressions

原题:

If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing by d, i.e., aa + da + 2da + 3da + 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 ad, 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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值