この「疑似コード」を Java で動作するものに変えたいのですが、問題が発生しています。
for j = i², i²+i, i²+2i, ..., not exceeding n:
これは正しいでしょうか?
for (int j = i*i; j < n; j++) {
//other code here that does the operation:
isPrime[j] = false;
j = j+i;
}
あなたが欲しいのはこれです:
for (int j = i * i; j < n; j += i)
{
isPrime[j] = false;
}
最初の問題は、j を 2 回インクリメントしていることです。宣言で 1 回、ループの最後でもう一度。やってみました:
for (int j = i*i; j < n; j+=i) {
//other code here that does the operation:
isPrime[j] = false;
}