Enter a number to determine whether it is prime. Three methods (Java)

1.Method 1

A prime number can only be divisible by 1 and itself. If it can divide a number from 2 to (i -1), it means that the number is not a prime number.

774103c997a64b1cb84dee855620cea6.png

 

2. Method 2

If a number is n, then it can be written in the form of a×b; for example, 16=1×16, which is equal to 4×4, 2×8, and these values ​​are equal to 16, then they can be written in the form of a×b, At this time we find that there must be a number that is less than or equal to n/2 (that is, less than or equal to 8). So anything greater than n/2 is a prime number.

0f79682d5f2042b4a3bb4f4e942dddc8.png

 

3.Method 3

If it can be written in the form of a into b, then we find that there must be a number less than or equal to the root n. Root 4=2; Root 8=2√2; Root 16=4, which is less than or equal to 4, so the range becomes 2~4. In method 1, this range was 2~15, in method 2 it was 2~8, and now it is 2~4.

65ce1d636c044bb188b68e6f3141bc0d.png

 

Guess you like

Origin blog.csdn.net/m0_56911648/article/details/129748317