import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Wu Ruixi
* Date:2019/10/17
* Time:16:55
**/
public class Work2_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数:");
int src = sc.nextInt();
System.out.println(isPrime(src) );
sc.close();
}
private static boolean isPrime(int src) {
double sqrt = Math.sqrt(src);
if (src < 2) {
return false;
}
if (src == 2 || src == 3) {
return true;
}
if (src % 2 == 0) {// 先判断是否为偶数,若偶数就直接结束程序
return false;
}
for (int i = 3; i <= sqrt; i+=2) {
if (src % i == 0) {
return false;
}
}
return true;
}
}
判定一个数字是否是素数
最新推荐文章于 2024-02-26 20:44:39 发布