LanQiaoTestCodes/素数的判断.java

26 lines
502 B
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package Course_Codes;
import java.util.Scanner;
public class 素数的判断 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()) {
int n=sc.nextInt();
System.out.println(isprime(n)?"Yes":"No");
}
}
public static Boolean isprime(int n) {
//排除特殊情况0 1
if(n<1)
return false;
//i*i<n 无需从2——n全部遍历只需遍历其一半即可
for(int i=2;i*i<n;i++)
if(n%i==0)
return false;
return true;
}
}