题解 | #判断质数#
判断质数
http://www.nowcoder.com/practice/4d2a100c2e544f56ac1ad17e9ffa107d
import java.util.Scanner;
public class Main { public static void main(String[] args) { Main main = new Main(); Scanner scan = new Scanner(System.in); int number = scan.nextInt(); System.out.println(main.isPrimeNumber(number)); }
public Boolean isPrimeNumber(int number) {
//write your code here......
//质数:一个数的约数只有1和它本身
if(number < 2 || number % 2 == 0){
return false;
}else if(number == 2){
return true;
}else{
for(int i = 3;i <= Math.sqrt(number);i += 2){
if(number % i == 0){
return false;
}
}
return true;
}
}
}