我正在尝试生成十个大素数。这是我的代码。我使用小数字来查看它是否正常工作。
public static void primeGenerator(){
BigInteger[] primeList = new BigInteger[10];
BigInteger startLine = new BigInteger("1");
int startPower = 1;
BigInteger endLine = new BigInteger("10");
int endPower = 2;
int j = 0;
for (BigInteger i = startLine.pow(startPower);
i.compareTo(endLine.pow(endPower)) <= 0;
i = i.add(BigInteger.ONE)) {
if(checkPrimeFermat(i) == true && j<10)
primeList[j] = i;
j++;
continue;
}
System.out.print(primeList[3]);
}
输出:
primeList[0] = null primeList[1] = 2 primeList[2] = 3 primeList[3] = null
我想要生成的输出:
primeList[0] = 2 primeList[1] = 3 primeList[2] = 5 primeList[3] = 7
当j出现时,4代码没有检查5并停在这里。我怎么解决这个问题?我用checkit测试了checkPrimeFermat,顺便说一句。
答案 0 :(得分:0)
这个工作:
public static void primeGenerator() {
BigInteger[] primeList = new BigInteger[10];
BigInteger startLine = new BigInteger("1");
int startPower = 1;
BigInteger endLine = new BigInteger("10");
int endPower = 2;
int j = 0;
for (BigInteger i = startLine.pow(startPower); i.compareTo(endLine
.pow(endPower)) <= 0; i = i.add(BigInteger.ONE)) {
if (checkPrimeFermat(i) == true && j < 10) {
primeList[j] = i;
j++;
}
}
System.out.print(Arrays.toString(primeList));
}