import java.util.Scanner;
public class hvosdov {
/**输入两个整数 输出两个整数之间的所有质数
*这里没有对 1 进行特别的声明
* @param args
*/
public static void main(String[] args) {
Scanner inqScanner = new Scanner(System.in);
Scanner inwScanner = new Scanner(System.in);
int inq = inqScanner.nextInt();
int inw = inwScanner.nextInt();
if (inq > inw) {
}else {
int c = inq;
inq = inw;
inw = c;
}
int[] ina = new int[inq-inw];
for (int i = inw; i < ina.length+inw; i++) {
ina[i-inw] = i;
}
for (int i = 0; i < ina.length; i++) {
if (ina[i]==-1) {
continue;
}
for (int j = 2; j < ina[i]; j++) {
if (0 == ina[i]%j) {
//这个就是一个合数 去除合数的倍数的数
for (int j2 = 1; j2 <= (inq-1)/ina[i]; j2++) {
ina[(j2*ina[i] )- inw] = -1;
}
}
}
}
StringBuffer stringBufferaBuffer = new StringBuffer();
stringBufferaBuffer.append("[");
for (int i = 0; i < ina.length; i++) {
if (ina[i]==-1) {
continue;
}
stringBufferaBuffer.append(ina[i]+",");
}
stringBufferaBuffer = new StringBuffer(stringBufferaBuffer.substring(0, stringBufferaBuffer.length()-1));
stringBufferaBuffer.append("]");
System.out.println(stringBufferaBuffer.toString());
}
}
自己改造了一个数组,用 正数 - 合数 = 质数 的思想来给中间代码优化。