[Algorithm] Determine whether a number is prime

[Thinking] A
prime number refers to a natural number greater than 1, except for 1 and the natural number without other factors, called a prime number.
If the number N is less than 2, it must not be a prime number.
If N can divide any integer i from 0 to N, then N is not a prime number.
If N can divide the square of i, then N must also divide i. Therefore, i*i<=N can be used as the judgment condition for the end of the loop to simplify the calculation.

【Code】

public static boolean isPrime(int N){
    
    
	if(N < 2) return false;
	for(int i = 2; i * i <= N; i++){
    
    
		if(N % i == 0) return false;
	}
	return true;
}

Guess you like

Origin blog.csdn.net/weixin_42020386/article/details/106724484