-1

so I need help figuring it out why my code is not including the number 2 and it is including number 99 on the prime printed line. Do I need to change something on my findPrime()? I tried playing with the index and just got worse.

    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);
  }
}

It is displaying this, instead of having the number 2 at the beginning and not having the number 99 at the last line of the program.

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
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
  • 2
    You need to debug this code. Please have a look at [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Hovercraft Full Of Eels Oct 17 '18 at 23:07
  • 1
    Also please check out [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). It won't solve your direct problem, but it will give you steps that you can follow that should help you solve it yourself, or even if that is not successful, then at least help you to better isolate your problem so that your question can be more focused and easier to answer. – Hovercraft Full Of Eels Oct 17 '18 at 23:07
  • It's because you said that `2` is not a prime with this line: `numbers[1] = false;`. You need to decide whether you want to index your array by the number directly, or by `number - 1`. Right now you're doing both at different points, and that naturally leads to errors. – Erwin Bolwidt Oct 17 '18 at 23:11
  • I deleted that line of code and still give me the same output. – Raul Cervantes Oct 17 '18 at 23:13
  • How is "giving you wrong output" different from "error"? An error is not just a "compiler error", it is "it is not doing what it is specified to do" – Erwin Bolwidt Oct 17 '18 at 23:13
  • "I deleted that line of code and still give me the same output." - I didn't say that you should delete the line. Think about it more. – Erwin Bolwidt Oct 17 '18 at 23:13

2 Answers2

0

Your toString() method starts looping at num = 2 (which appends num+1 to the output). Your loop should start at 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();
    }
}

Plus your code sets numbers[1] = false. That should be numbers[1] = true.

You are also looping until < max - 1. Consider looping until < max.

Jason
  • 11,744
  • 3
  • 42
  • 46
0

I made a few changes to your code the big things to point out is that you don't need max. Your findPrimes() looks ok but is difficult to read and hard to verify for correctness. Your toString() method should be iterating over every element and if that element is true append it to the list.

also 1 is not prime so numbers[1] = false; is as it should be. 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);
    }
}
silverduck
  • 401
  • 6
  • 9