Description
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).
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.
解题思路
找出给出的范围内,相邻最近和最远的两个素数,用到了筛素数法,因为 1 <= L < U <= 2,147,483,647 所以只要先找出50000以内的素数,再对给出的范围再次筛选就行了。
因为范围的数值较大,所以数组记录素数时要用到映射。
AC代码
#include <cstdio>
#include <cstring>
#define maxn 1000001
#define LL long long
LL p_list[50000], p_cnt; //p_list[]存储50000范围内的素数,p_cnt计数
LL map[maxn], p_map[maxn], count; //map[]存储给定范围内素数的情况p_map[],count计数
void get_prime()
{
LL p_tmp[50000];
memset(p_tmp, 0, sizeof(p_tmp));
for(int i = 2; i < 50000; i++)
{
if( p_tmp[i] == 0 )
{
p_list[p_cnt++] = i;
for(int k = i * 2; k < 50000; k += i)
p_tmp[k] = 1;
}
}
}
int main()
{
LL a, b;
get_prime();
while(scanf("%lld%lld", &a, &b) != EOF)
{
if(a == 1)
a++;
memset(map, 0, sizeof(map));
for(LL i = 0; i < p_cnt && p_list[i] * p_list[i] <= b; i++) //筛选[a, b]内的素数,map[]记录筛选结果
{
LL tmp = a / p_list[i];
if(tmp * p_list[i] != a)
tmp++;
if(tmp == 1)
tmp++;
for(LL j = tmp * p_list[i]; j <= b; j += p_list[i])
map[j - a] = 1; //映射
}
count = 0;
for(LL i = a; i <= b; i++) //p_map[]记录筛选出来的素数
if(map[i - a] == 0)
p_map[count++] = i;
if(count < 2)
printf("There are no adjacent primes.\n");
else
{
LL m1 = p_map[0], m2 = p_map[1], n1 = p_map[0], n2 = p_map[1]; //找出相邻最近和最远的两个素数
for(int i = 2; i < count; i++)
{
if(p_map[i] - p_map[i - 1] < m2 - m1)
m2 = p_map[i], m1 = p_map[i - 1];
if(p_map[i] - p_map[i - 1] > n2 - n1)
n2 = p_map[i], n1 = p_map[i - 1];
}
printf("%lld,%lld are closest, %lld,%lld are most distant.\n", m1, m2, n1, n2);
}
}
return 0;
}
<pre name="code" class="csharp">