*/
Public class ForTest05 {
Public static void main (String [] args) {
A Boolean flag=true;//is prime
for(int i=2; i<100; I++) {
For (int j=2; jIf (I % j==0) {
Flag=false;
break;
}
}
If (flag==true) {
System.out.println(i);
}
}
}
}
You tell me why is finally concluded that the 2 and 3.
A Boolean flag=true; Why, in the for loop inside and outside is not the same ah, outside also can be applied to the for loop, isn't it, why the answer is not the same as
CodePudding user response:
public class test {
Public static void main (String [] args) {
A Boolean flag=true;
For (int I=2; I & lt;=100; I++) {
For (int j=2; J & lt;=I - 1; J++) {
If (I % j==0) {
Flag=false;
break;
}
}
If (flag) {
System.out.println(i);
}
Flag=true;
}
}
}
CodePudding user response:
public class test {
Public static void main (String [] args) {
For (int I=2; I & lt;=100; I++) {
boolean flag=true;
For (int j=2; J & lt;=I - 1; J++) {
If (I % j==0) {
flag=false;
break;
}
}
If (flag) {
System.out.println(i);
}
}
}
}
CodePudding user response:
I in the loop again to assignment of flag,CodePudding user response:
Public class Test {Public static void main (String [] args) {
for(int i=2; I & lt;=100; I++) {
boolean flag=true;
For (int j=2; J & lt; (int) (Math. SQRT (I) + 1); J++) {
Flag=I % j!=0;
if(! Flag) {
break;
}
}
If (flag) {
System.out.println(i);
}
}
}
}
CodePudding user response:
CodePudding user response:
Write a optimized, the for loop half:
Public ListCCC () {
ListRet=new ArrayList<> (a);
for (int i=0; I & lt; 100; I++) {
If (isPrime (I))
Ret. Add (I);
}
return ret;
}
The static Boolean isPrime (int num) {
If (num & lt; 2)
return false;
If (num & lt; 4)//2 and 3 is a prime
return true;
If (num % 2==0)
return false;
//I +=2 half cycle
For (int I=3, end=(int) (math.h SQRT (num) + 1); I & lt; end; I +=2) {
If (num % I==0)
return false;
}
return true;
}
CodePudding user response:
Had written a bitmap is used to implement, can find all the prime Numbers within 200 million in 4 seconds, but is written in c #, reference:///& lt; summary>
///to find all the prime Numbers specified interval return.
///note 1: maxValue support to 200 million only, if you need more than 200 million, please consider split two arrays, a new implementation
///note 2: batch to find prime Numbers within one million, this method only need 5 milliseconds, cycle call IsPrime to 200 milliseconds.
///there are 11078937 primes within 200 million, this method takes about four seconds
///& lt;/summary>
///& lt; Param name="maxValue & gt;"
///& lt; Param name="minValue & gt;"
///& lt; returns>
Public static ListGetPrimes (int maxValue=https://bbs.csdn.net/topics/200000000, int minValue=2)
{
If (maxValue & lt; MinValue)
{
Throw new ArgumentException (" minimum value is greater than the maximum ");
}
If (maxValue & gt; 200000000)
{
Throw new ArgumentException (" maximum should not exceed 200 million, avoid out of memory ");
}
//starting from 0, so the result of reference, to reduce 1
Var result=new bool [maxValue];
The result of [2-1]=true;
The result of [3-1]=true;
//marker for prime multiples of 2
For (var I=5; I & lt;=maxValue; I +=2)
{
Result [I - 1]=true;
}
Var SQRT=math.h SQRT (maxValue);
For (var I=3; I & lt;=SQRT; I++)
{
If (result] [I - 1)
{
//tag in multiples of the current prime is a prime
For (var inner=I + I; Inner & lt;=maxValue; Inner +=I)
{
The result [inner - 1]=false;
}
}
}
Var ret=new List(a);
For (var I=minValue; I & lt;=maxValue; I++)
{
If (result] [I - 1)
{
Ret. Add (I);
}
}
return ret;
}
CodePudding user response:
Package studyProgram;import java.util.Scanner;
/* *
*
* @ content: find a range of prime number
* @ author: H Yang X
* @ time: on February 20, 2020 in the afternoon 8:23:02
*/
Public class JudgePrimeNumber {
Private static Scanner input;
Public static void main (String [] args) {
Int start=0, end=0, newline=0, count=0;
Input=new Scanner (System. In);
System. The out. Println (" the System will help you find out you plan range of prime Numbers, please follow the instructions below: ");
System. The out. Println (" input initial interval number: ");
Start=input. NextInt ();
System. The out. Println (" please input termination of interval number: ");
End=input. NextInt ();
System. Out. Println (start + "~ + end" + "between prime Numbers are:");
For (int I=start; I & lt;=end; I++) {
boolean flag=true;//is prime
For (int j=2; J & lt; i; J++) {
If (I % j==0) {
flag=false;
break;
}
}
If (flag) {
count++;
If (newline++ % 10==0)
System.out.println(); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull