Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想把这个“伪代码”变成在 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 递增两次。一次在声明中,再次在循环结束时。你有没有尝试过:
for (int j = i*i; j < n; j+=i) { //other code here that does the operation: isPrime[j] = false; }