其他分享
首页 > 其他分享> > PAT 2019-3 7-1 Sexy Primes

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:

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 }

 

标签:prime,PAT,Sample,Output,2019,Input,print,sexy,Primes
来源: https://www.cnblogs.com/blue-lin/p/11443383.html