- import java.util.Scanner;
- public class IsPrime {
- private static Scanner sc;
-
- public static void main(String[] args) {
-
- sc = new Scanner(System.in);
- long n = sc.nextLong();
- if(isPrime(n)){
- System.out.println(n+" 是素数");
- }else{
- System.out.println(n+ " 不是素数");
- }
- }
- public static boolean isPrime(long n) {
- if (n <= 3) {
- return n > 1;
- }
- if (n % 2 == 0 || n % 3 == 0) {
- return false;
- }
- for (int i = 5; i <= Math.sqrt(n); i += 6) {
- if (n % i == 0 || n % (i + 2) == 0) {
- return false;
- }
- }
- return true;
- }
- }
复制代码 |