Question requirements: in the given range L ~ Find the two prime numbers with the largest difference and the smallest difference in R.
To ~ R filters prime numbers and calculates 1 ~ 2,147,483,647 will definitely time out.
But in fact, filter 1 ~ There are not many factors required for prime numbers in 2,147,483,647, As long as 2 ~ The prime number in sqrt (2,147,483,647) is enough. This is what the limit method does.
Then use these prime numbers ~ Division of R to obtain L ~ Result of prime number in R
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace std;
Bool Q [1001000];
Bool p [50000];
Int P [5000];
Int main ()
{
_ Int64 I, j, k, L, R;
For (I = 2; I <= 50000; I + = 2) p [I] = 0, p [I + 1] = 1; p [1] = 0; p [2] = 1; P [1] = 2; P [0] = 1;
For (I = 1; I <= 46345; I + = 2)
{
If (p [I])
{
Int k = I * I;
While (k <50000 ){
P [k] = 0;
K + = 2 * I;
}
P [++ P [0] = I;
}
}
While (scanf ("% I64d % I64d", & L, & R )! = EOF)
{
Int Start;
For (I = L; I <= R; I ++) Q [I-L] = 1;
For (I = 1; I <= P [0]; I ++)
{
If (P [I]> = R) break;
Start = L/P [I];
If (Start * P [I] <L) Start ++;
If (Start = 1) Start ++;
For (j = Start; j * P [I] <= R; j ++) Q [j * P [I]-L] = 0;
}
If (L = 1) Q [0] = 0, Q [2] = 1;
If (L = 2) Q [0] = 1;
_ Int64 pre = R, now = 0, min = 1000000000, max = 0, a, B, x, y;
For (I = L; I <= R; I ++ ){
If (Q [I-L]) {
Pre = I;
Break;
}
}
For (I = pre + 1; I <= R; I ++)
{
If (Q [I-L]) {
Now = I;
If (now-pre <min ){
Min = now-pre;
A = pre, B = now;
}
If (now-pre> max ){
Max = now-pre;
X = pre, y = now;
}
Pre = now;
}
}
If (! Now) printf ("There are no adjacent primes. \ n ");
Else printf ("% I64d, % I64d are closest, % I64d, % I64d are most distant. \ n", a, B, x, y );
}
Return 0;
}
/*
2000000000 2000000001
2146483648 2147483647
*/
# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace std;
Bool Q [1001000];
Bool p [50000];
Int P [5000];
Int main ()
{
_ Int64 I, j, k, L, R;
For (I = 2; I <= 50000; I + = 2) p [I] = 0, p [I + 1] = 1; p [1] = 0; p [2] = 1; P [1] = 2; P [0] = 1;
For (I = 1; I <= 46345; I + = 2)
{
If (p [I])
{
Int k = I * I;
While (k <50000 ){
P [k] = 0;
K + = 2 * I;
}
P [++ P [0] = I;
}
}
While (scanf ("% I64d % I64d", & L, & R )! = EOF)
{
Int Start;
For (I = L; I <= R; I ++) Q [I-L] = 1;
For (I = 1; I <= P [0]; I ++)
{
If (P [I]> = R) break;
Start = L/P [I];
If (Start * P [I] <L) Start ++;
If (Start = 1) Start ++;
For (j = Start; j * P [I] <= R; j ++) Q [j * P [I]-L] = 0;
}
If (L = 1) Q [0] = 0, Q [2] = 1;
If (L = 2) Q [0] = 1;
_ Int64 pre = R, now = 0, min = 1000000000, max = 0, a, B, x, y;
For (I = L; I <= R; I ++ ){
If (Q [I-L]) {
Pre = I;
Break;
}
}
For (I = pre + 1; I <= R; I ++)
{
If (Q [I-L]) {
Now = I;
If (now-pre <min ){
Min = now-pre;
A = pre, B = now;
}
If (now-pre> max ){
Max = now-pre;
X = pre, y = now;
}
Pre = now;
}
}
If (! Now) printf ("There are no adjacent primes. \ n ");
Else printf ("% I64d, % I64d are closest, % I64d, % I64d are most distant. \ n", a, B, x, y );
}
Return 0;
}
/*
2000000000 2000000001
2146483648 2147483647
*/