Prime Distance (大素数 -- 双筛)

The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no proper factors (it is only evenly divisible by 1 and itself). The first prime numbers are 2,3,5,7 but they quickly become less frequent. One of the interesting questions is how dense they are in various ranges. Adjacent primes are two numbers that are both primes, but there are no other prime numbers between the adjacent primes. For example, 2,3 are the only adjacent primes that are also adjacent numbers.
Your program is given 2 numbers: L and U (1<=L< U<=2,147,483,647), and you are to find the two adjacent primes C1 and C2 (L<=C1< C2<=U) that are closest (i.e. C2-C1 is the minimum). If there are other pairs that are the same distance apart, use the first pair. You are also to find the two adjacent primes D1 and D2 (L<=D1< D2<=U) where D1 and D2 are as distant from each other as possible (again choosing the first pair if there is a tie).
Input
Each line of input will contain two positive integers, L and U, with L < U. The difference between L and U will not exceed 1,000,000.
Output
For each L and U, the output will either be the statement that there are no adjacent primes (because there are less than two primes between the two given numbers) or a line giving the two pairs of adjacent primes.
Sample Input
2 17
14 17
Sample Output
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.

//筛 两 次 就 好 ,用已有素数 筛 区间素数;
//n-m区间 的数的 最大约数 不超过 根号m

#pragma warning(disable:4996)
#include"iostream"
#include"functional"
#include"algorithm"
#include"cstring"
#include"stack"
#include"cmath"
#include"queue"
#include"vector"
#include"map"
typedef long long int ll;
using namespace std;
ll prime[200009], vis[200009], cnt = 0, prime1[2000009], vis1[2000009], cnt1 = 0;
void isprime() {
	vis[0] = 1;
	vis[1] = 1;
	for (int i = 2; i < 200000; i++) {
		if (!vis[i]) {
			prime[cnt++] = i;
			for (int j = i * 2; j < 200000; j += i) {
				vis[j] = true;
			}
		}

	}
}
void isprime1(ll a, ll b) {
	memset(vis1, 0, sizeof(vis1));
	ll py = a - 1;
	cnt1 = 0;
	ll l = 0, r = 0;
	if (a < 2) a = 2;
	for (int i = 0; i < cnt; i++) {
		l = a / prime[i];
		while (l * prime[i] < a) l++;
		if (l == 1) l++;
		for (int j = l; prime[i] * j <= b; j++) {
			vis1[prime[i] * j - py] = 1;
		}
	}
	for (int i = 1; i <= b - py; i++) {
		if (i + py == 1) continue;
		if (!vis1[i]) prime1[cnt1++] = i + py;
	}

}

int main() {
	ll a, b;
	isprime();
	while (~scanf("%lld%lld", &a, &b)) {
		isprime1(a, b);
		ll ma[3], mi[3], m = 0;
		ll p = lower_bound(prime1, prime1 + cnt1, a) - prime1;
		int i = p + 2, mm = 0, pp=0;
		if (p+1 >= cnt1) {
			cout << "There are no adjacent primes." << endl;
			continue;
		}
		else {
			ma[0] = mi[0] = prime1[p];
			ma[1] = mi[1] = prime1[p + 1];
			mm = prime1[p + 1] - prime1[p];
			pp = prime1[p + 1] - prime1[p];
		}
		for (; i<cnt1; i++) {
			if (prime1[i] - prime1[i - 1] > mm) {
				mm = prime1[i] - prime1[i - 1];
				ma[0] = prime1[i - 1];
				ma[1] = prime1[i];
			}
			if (prime1[i] - prime1[i - 1] < pp) {
				pp = prime1[i] - prime1[i - 1];
				mi[0] = prime1[i - 1];
				mi[1] = prime1[i];
			}
		}
		cout << mi[0] << "," << mi[1] << " are closest, " << ma[0] << "," << ma[1] << " are most distant." << endl;
	}
}
发布了49 篇原创文章 · 获赞 0 · 访问量 660

猜你喜欢

转载自blog.csdn.net/qq_14989799/article/details/104741672