当前位置:网站首页>Acwing summer daily question (sexy prime number on June 10)

Acwing summer daily question (sexy prime number on June 10)

2022-06-12 15:16:00 Dashuaibi's junior attendant

Sexy prime  ” It is shaped like  (p,p+6)(p,p+6)  Such a pair of prime numbers .

It's called , Because the Latin tube “ 6、 ... and ” It's called “sex”( That is, English “ sexy ”).

Now give an integer , Please judge whether it is a sexy prime .

Input format

Input gives a positive integer on a line NN.

Output format

if NN It's a sexy prime , Output in one line Yes, And output and on the second line NN Another sexy prime paired ( If such a number is not unique , The one with the smaller output ).

if NN Not a sexy prime , Output in one line No, Then output greater than... On the second line NN The minimum number of sexy prime .

Data range

1≤N≤10^8

Their thinking : First, use trial division to screen prime numbers . If it's a sexy prime number , Output “Yes” And sexy prime , If not , Then violence goes to the nearest sexy prime number , guess : The nearest one must not be far away . If possible Ac Just go , Otherwise, it needs to be optimized .

Code :

import java.util.*;
import java.io.*;

public class Main{
    public static boolean is_prime(int n){
        if(n <= 2){
            return false;
        }
        for(int i = 2 ; i <= n / i ; i++){
            if(n % i == 0){
                return false;
            }
        }
        return true;
    }
    public static void main(String args[]) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        for(int i = n - 6 ; i <= n + 6 ; i += 12){
            if(is_prime(i) && is_prime(n)){
                System.out.println("Yes");
                System.out.println(i);
                return;
            }
        }
        
        for(int i = n + 1 ;; i++){
            if(is_prime(i) && (is_prime(i-6) || is_prime(i+6))){
                System.out.println("No");
                System.out.println(i);
                return;
            }
        }
        
    }
}

summary :

I haven't written algorithms for a long time , I / O is jammed , Hands are too raw , Adhere to the

原网站

版权声明
本文为[Dashuaibi's junior attendant]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121509484792.html