判断一个数是否是素数 Java

2023-07-30 10:44:47 浏览数 (2)

代码语言:javascript复制
import java.util.Scanner;
import java.lang.Math;
public class studying {
    private static boolean isprime(int target){
        if(target<2)
            return false;
        for(int i=2;i<=Math.sqrt(target);i  )
            if(target%i==0)
                return false;
        return true;
    }
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int target=input.nextInt();
        if(isprime(target))
            System.out.println("The number is a prime number!");
        else
            System.out.println("The number is not a prime number!");
    }
}

0 人点赞