Dirichlet’s Theorem on Arithmetic Progressions(素数判断算法的探究)

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12763 Accepted: 6419

Description

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 the nth prime number in this arithmetic sequence for given positive integers ad, and n.

Input

The input is a sequence of datasets. A dataset is a line containing three positive integers ad, and n separated by a space. a and d are relatively prime. You may assume a <= 9307, d <= 346, and n <= 210.

The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.

Output

The output should be composed of as many lines as the number of the input datasets. Each line should contain a single integer and should never contain extra characters.

The output integer corresponding to a dataset adn should be the nth prime number among those contained in the arithmetic sequence beginning with a and increasing by d.

FYI, it is known that the result is always less than 106 (one million) under this input condition.

Sample Input

367 186 151
179 10 203
271 37 39
103 230 1
27 104 185
253 50 85
1 1 1
9075 337 210
307 24 79
331 221 177
259 170 40
269 58 102
0 0 0

Sample Output

92809
6709
12037
103
93523
14503
2
899429
5107
412717
22699
25673
解:
这道题重点是素数的求法,素数常规有3种方法,设待判断的数x,一种是i = 2; i <= x,第二种是i = 2; i <= x/2,第三种是i = 2; i <= sqrt((float)x)。当然更快的还有筛法,这里使用常规的第三种和筛法做为示例,看看效果如何。
首先是第三种是i = 2; i <= sqrt((float)x)的代码和结果:
#include <stdio.h>
#include <math.h>
bool isPrime(int x) {
	if(x < 2) {
		return false;
	} else {
		for(int i = 2; i <= sqrt((float)x); i++) {
			if(x % i == 0) {
				return false;
			}
		}
		return true;
	}
}

int main()
{
	int a, d, n;
	while (scanf("%d %d %d", &a, &d, &n) != EOF) {
		if (a == 0 && d == 0 && n == 0) {
			return 0;
		}
		int i, count = 0;
		for (i = a; count < n; i += d) {
			if (isPrime(i)) {
				count++;
			}
		}
		printf("%d\n", i - d);
	}
	return 0;
}
Result Memory Time Language Code Length
Accepted 188K 250MS C++ 508B
这个方法内存使用了188K,时间使用了250MS,时间用得还是算较多的。
接下来是筛法来计算素数,代码和结果如下:
#include <stdio.h>
#include <math.h>
#define N 1000000
bool prime[N];

void generatePrime() {
	int i, j;
	prime[0] = false;
	prime[1] = false;
	for (i = 2; i < N; i++) {
		if (i % 2 == 0 && i != 2) {
			prime[i] = false;
		} else {
			prime[i] = true;
		}
	}
	for (i = 3; i <= sqrt((float)N); i++) {
		if (prime[i]) {
			for (j = 3 * i; j < N; j += 2 * i) {
				prime[j] = false;
			}
		}
	}
}

int main()
{
	generatePrime();
	int a, d, n;
	while (scanf("%d %d %d", &a, &d, &n) != EOF) {
		if (a == 0 && d == 0 && n == 0) {
			return 0;
		}
		int i, count = 0;
		for (i = a; count < n; i += d) {
			if (prime[i]) {
				count++;
			}
		}
		printf("%d\n", i - d);
	}
	return 0;
}
Result Memory Time Language Code Length
Accepted 1164K 16MS C++ 719B
这个方法内存使用了1164K,时间使用了16MS,时间用得非常非常少,而内存却用了挺多的,但是我们ACM主要看时间,用空间换取时间,这个排名已经很靠前了。这个方法是从《素数判断算法(高效率)》一文中学习来的,我在它的基础上进行了修改,其实原文的算法是有几个错误的,比如0,1,2没有判断掉,原文的 for (j = i + i; j < N; j += i)被我用for (j = 3 * i; j < N; j += 2 * i),因为i + i = 2i,这个其实在上面的模2的时候已经判断过了,所以可以跳得大步点,j += i这个改成j += 2 * i是配合我改的这个,这样跳得就比之前大步多了,理论上可以少判断很多的数。

 

Tonitech版权所有 | 转载请注明出处: http://www.tonitech.com/1761.html

发表评论