Why use the Math. sqrt (I) method and the math. sqrt method?
Java exercise questions
Determine the number of prime numbers between 101-200 and output all prime numbers.
Public class Prime {
Public static int count = 0;
Public static void main (String [] args ){
For (int I = 101; I <200; I ++ ){
Boolean B = true;
For (int j = 2; j <= Math. sqrt (I); j ++ ){//---------------
If (I % j = 0 ){
B = false;
Break;
}
}
If (B ){
System. out. print (I + "");
Count ++;
}
}
System. out. println ("\ n prime number:" + count );
}
}
It is found that there is always a problem in the second for loop without Math. sqrt (I ).
Then I found this problem: Why should I use the Math. sqrt (I) method (return the positive square root of the correctly rounded double value )?
Because, you only need to judge this value,
For example, to judge 100, you only need to judge 10, 10*10
100 = n1 * n2, so there must be one n1 or n2 <= 10, so you only need to judge 10
Improve Efficiency
NOTE: If your personal opinion is incorrect, you can also point it out.