# 7-4 判断素数 (10分)

​31
​​ 的需要判断的正整数。

2
11
111

Yes
No

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.StringTokenizer;

//** Class for buffered reading int and double values *//*
static StringTokenizer tokenizer;

// ** call this method to initialize reader for InputStream *//*
static void init(InputStream input) {
tokenizer = new StringTokenizer("");
}

// ** get next word *//*
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
// TODO add check for eof if necessary
}
}
static boolean hasNext()throws IOException {
}
static String nextLine() throws IOException{
}
static char nextChar() throws IOException{
return next().charAt(0);
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}

static float nextFloat() throws IOException {
return Float.parseFloat(next());
}
}
public class Main {
public static void main(String[] args) throws IOException {
for (int i = 0; i < n; i++) {
if (isPrime(m)) {
System.out.println("Yes");
}else {
System.out.println("No");
}
}
}

public static boolean isPrime(int num) {
if(num==2||num==3) {
return true;
}
if (num==1||num%6!=1&&num%6!=5) {
return false;
}
int temp = (int) Math.sqrt(num);
for (int i = 5; i < temp; i+=6) {
if (num%i==0||num%(i+2)==0) {
return false;
}
}
return true;
}

}



©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试