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 varous ranges. adjacent primes are two numbers that are both primes, but there are no other prime numbers between the adjacent primes. for example, 2 and 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 1714 17
Sample output
2,3 are closest, 7,11 are most distant.There are no adjacent primes.
Solution:
I have been doing this with tears. I kept fighting, it was difficult to optimize it, and RE, the code was also very well written. Is the normal Secondary Filtering prime number. Because of the large data size, the first time a prime number of less than 46500 is screened out, then the prime number in the interval is filtered out based on this.
Note: although the given number does not exceed the int range, the multiplication of the two numbers will exceed the int range, and I will re here.
AC code:
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> using namespace STD; const int n = 1000005; const int M = 46500; const int INF = 999999999; bool notprime [N]; int prime_1 [M + 1], prime_2 [N]; int num_1 = 0, num_2; void prime1 () // prime number {memset (notprime, false, sizeof (notprime); For (INT I = 2; I <= m; I ++) if (! Notprime [I]) {prime_1 [num_1 ++] = I; for (Int J = 2 * I; j <= m; j + = I) notprime [J] = true ;}} void prime2 (int l, int U) // the second time the prime number in the given range is screened out {memset (notprime, false, sizeof (notprime); num_2 = 0; If (L <2) L = 2; int K = SQRT (u * 1.0); For (INT I = 0; I <num_1 & prime_1 [I] <= K; I ++) {int T = L/prime_1 [I]; If (T * prime_1 [I] <L) t ++; If (T <= 1) t = 2; for (Int J = T; (long) J * prime_1 [I] <= u; J ++) // The multiplication result is out of the range. Use long notprime [J * prime_1 [I]-l] = 1 ;}for (INT I = 0; I <= u-l; I ++) if (! Notprime [I]) prime_2 [num_2 ++] = I + L;} int main () {int L, U, DIS, A_1, B _1, A_2, B _2, B _2, Minn, maxx; prime1 (); While (scanf ("% d", & L, & U )! = EOF) {Minn = inf, Maxx =-1; prime2 (L, U); If (num_2 <2) {printf ("there are no adjacent primes. \ n "); continue;} For (INT I = 1; I <num_2 & prime_2 [I] <= u; I ++) {Dis = prime_2 [I]-prime_2 [I-1]; If (DIS> Maxx) {A_1 = prime_2 [I-1]; A_2 = prime_2 [I]; maxx = DIS;} If (DIS <Minn) {B _1 = prime_2 [I-1]; B _2 = prime_2 [I]; Minn = DIS;} printf ("% d, % d are closest, % d, % d are most distant. \ n ", B _1, B _2, A_1, A_2);} return 0 ;}