PAT 2019-3 7-1 Sexy Primes

Description:

Sexy primes are pairs of primes of the form (p, p+6), so-named since "sex" is the Latin word for "six". (Quoted from http://mathworld.wolfram.com/SexyPrimes.html)

Now given an integer, you are supposed to tell if it is a sexy prime.

Input Specification:

Each input file contains one test case. Each case gives a positive integer N (≤).

Output Specification:

For each case, print in a line Yes if N is a sexy prime, then print in the next line the other sexy prime paired with N (if the answer is not unique, output the smaller number). Or if N is not a sexy prime, print No instead, then print in the next line the smallest sexy prime which is larger than N.

Sample Input 1:

47

Sample Output 1:

Yes
41

Sample Input 2:

21

Sample Output 2:

No
23

Keys:

  • 素数

Attention:

  • 这几次很爱考素数,奶一口欧拉算法,9月份考试能不能中

Code:

 1 #include<cstdio>
 2 typedef long long LL;
 3 
 4 bool IsPrime(LL n)
 5 {
 6     if(n <= 1)
 7         return false;
 8     for(LL i=2; i*i<=n; i++)
 9         if(n%i == 0)
10             return false;
11     return true;
12 }
13 
14 int main()
15 {
16 #ifdef ONLINE_JUDGE
17 #else
18     freopen("Test.txt", "r", stdin);
19 #endif // ONLINE_JUDGE
20 
21     LL n,l,r,m;
22     scanf("%lld", &n);
23     if(IsPrime(n-6) && IsPrime(n))
24         printf("Yes\n%lld", n-6);
25     else if(IsPrime(n+6) && IsPrime(n))
26         printf("Yes\n%lld", n+6);
27     else
28     {
29         for(LL i=n+1; i<=1e9; i++)
30             if(IsPrime(i) && IsPrime(i-6))
31             {
32                 printf("No\n%lld", i);
33                 break;
34             }
35     }
36 
37     return 0;
38 }

猜你喜欢

转载自www.cnblogs.com/blue-lin/p/11443383.html