当前位置:网站首页>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
边栏推荐
- Leetcode daily question - fair candy bar exchange
- Deepin20.6 rtx3080 installing graphics card drivers 510.60.02, cuda11.6, pytorch1.11
- Left aligned, right aligned, random number, goto, compare output bool
- Phpstudy indicates that the hosts file may not exist or be blocked from being opened. How to resolve the failure of synchronizing hosts
- idea 拉取分支代码
- C constant, cannot be changed
- TCP/IP 三次握手四次挥手(面试题)
- USART (rs232422485), I2C, SPI, can, USB bus
- C operator
- Jetpack architecture component learning (3) -- activity results API usage
猜你喜欢
随机推荐
USART (rs232422485), I2C, SPI, can, USB bus
The process of generating strong association rules from frequent itemsets
Function related matters
Summary of advantages and disadvantages of various architectures
C escape character
[jvm learning] class loading subsystem
阿里、腾讯、拼多多垂范,产业互联网的新逻辑渐显
Selenium advanced
Ngork implements intranet penetration -- free
[SPARK][CORE] 面试问题之什么是 external shuffle service?
左对齐,右对齐,随机数,goto,比较输出bool
How to add WWW to the domain name
【无标题】
Learning is an inhumane thing (becoming an expert's internal mind skill)
Dart typedef的理解
Left aligned, right aligned, random number, goto, compare output bool
FIRSTVT和LASTVT白话版
Swap numbers, XOR, operator correlation
MH32F103ARPT6软硬件兼容替代STM32F103RCT6
idea 拉取分支代码







![[LDA] basic knowledge notes - mainly AE and VAE](/img/1c/ccc073cac79b139becd5de0b9a91b1.png)