所以我需要帮助弄清楚为什么我的代码在原始印刷行上不包括数字2,而是包括数字99。我是否需要更改findPrime()上的内容?我尝试使用索引,但情况变得更糟。
class Sieve {
private int max;
private boolean[] numbers;
public Sieve(int max) {
if (max < 2) {
throw new IllegalArgumentException();
}
this.max = max;
numbers = new boolean[max];
numbers[0] = false;
numbers[1] = false;
numbers[2] = true;
for (int i = 2; i < max-1; i++) {
numbers[i] = true;
}
}
public void findPrimes() {
for (int num = 2; num < max-1; num++) {
int multiples = num + num;
while (multiples < max-1) {
numbers[multiples-1] = false;
multiples += num;
}
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (int num = 2; num < max; num++) {
if (numbers[num]) {
builder.append(num+1).append(" ");
}
}
return builder.toString();
}
}
class Driver
{
// MAIN. Find some primes.
public static void main(String [] args)
{
Sieve sieve = null; // We must initialize SIEVE or Java will cry.
// 5 points. This must print "Sieve size must be at least 2." but without the
// quotes.
try
{
sieve = new Sieve(0);
}
catch (IllegalArgumentException oops)
{
System.out.println("Sieve size must be at least 2.");
}
// 5 points. This must print nothing.
try
{
sieve = new Sieve(100);
}
catch (IllegalArgumentException oops)
{
System.out.println("Sieve size must be at least 2.");
}
// 10 points. This must print integers from 2 to 99, separated by blanks.
System.out.println(sieve);
// 10 points. This must print the prime numbers between 2 and 99, separated by
// blanks. They are:
//
// 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
sieve.findPrimes();
System.out.println(sieve);
}
}
它正在显示此内容,而不是在程序的开头没有数字2且在最后一行没有数字99。
Sieve size must be at least 2.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 99
答案 0 :(得分:0)
您的toString()
方法以num = 2(将num + 1附加到输出)开始循环。您的循环应从1开始。
public String toString() {
StringBuilder builder = new StringBuilder();
for (int num = 1; num < max; num++) { . // used to start at 2
if (numbers[num]) {
builder.append(num+1).append(" ");
}
}
return builder.toString();
}
}
加上您的代码集numbers[1] = false
。应该是numbers[1] = true
。
您还将循环播放,直到< max - 1
。考虑循环直到< max
。
答案 1 :(得分:0)
我对您的代码做了一些更改,需要指出的是,您不需要max。您的findPrimes()看起来不错,但很难阅读且很难验证其正确性。您的toString()方法应该遍历每个元素,如果该元素为true,则将其添加到列表中。
另外1不是质数,所以numbers[1] = false;
应当是素数。 Why is 1 not a prime number?
class Sieve {
// private int max; // max is superfluous use numbers.length instead
private boolean[] numbers;
public Sieve(int max) {
if (max < 2) {
throw new IllegalArgumentException();
}
numbers = new boolean[max];
numbers[0] = false;
numbers[1] = false;
numbers[2] = true;
for (int i = 2; i <numbers.length; i++) {
numbers[i] = true;
}
}
public void findPrimes() {
// a bit more compact and neater in my opinion
for (int num=2; num<numbers.length; num++) {
for (int multiple=num+num; multiple<numbers.length; multiple+=num) {
numbers[multiple] = false;
}
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
// you should be iterating from 0 to 99
for (int i=0; i<numbers.length; i++) {
if (numbers[i]) {
builder.append(i).append(" ");
}
}
return builder.toString();
}
}
class Driver
{
// MAIN. Find some primes.
public static void main(String [] args)
{
Sieve sieve = null; // We must initialize SIEVE or Java will cry.
// 5 points. This must print "Sieve size must be at least 2." but without the
// quotes.
try
{
sieve = new Sieve(0);
}
catch (IllegalArgumentException oops)
{
System.out.println("Sieve size must be at least 2.");
}
// 5 points. This must print nothing.
try
{
sieve = new Sieve(100);
}
catch (IllegalArgumentException oops)
{
System.out.println("Sieve size must be at least 2.");
}
// 10 points. This must print integers from 2 to 99, separated by blanks.
System.out.println(sieve);
// 10 points. This must print the prime numbers between 2 and 99, separated by
// blanks. They are:
//
// 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
sieve.findPrimes();
System.out.println(sieve);
}
}