-3

It's about finding the prime numbers from 2 to 1000 using this method but I can't get the solution and I've been thinking and trying to solve this issue for three days. I'm desperate for help so if anyone can help me I would really appreciate it

I tried another for loop and an if statement since my teacher said that I only need another loop or just one more code line but I can't seem to get the solution. I'm really bad at this so I'm sorry if my code seems cringeworthy

public class Practica {

  public static void main(String []   
    byte []marcado = new byte [1000];
    for (int i = 2; i < 1000; i++);
    if (marcado[i] == 1) {
      for (int j = 2; i*j < 1000; j++) {
       marcado [i*j] = 0;
      }
    }

I expect to have all the prime numbers printed

LGV
  • 11
  • 2
  • JavaScript has nothing to do with Java, it's a completely separate language. As for your code, can you describe what exactly it's supposed to do? –  Feb 14 '19 at 19:41
  • I would step back and re-visit all the things you learned so far in your class. There are multiple problems in your code. For example: your first for loop, you have a trailing ; on that line. This means that the for loop has an EMPTY body. This means that your code ... should actually not even compile, because *i* isn't defined after that line. Put your for loop body into { braces } as well. And beyond taht, your current code just assigns 0s into an array, that has 0s in each slot anways. – GhostCat Feb 14 '19 at 19:44
  • 1
    OK there are lots of compile errors there. Work through them one by one, trying to make the code look like something you may have seen before. Particularly watch out for stray semicolons in places that there aren't supposed to be any. Also, you'll need a line of code that actually prints the prime number in the case where `marcado[i]` turns out to be 1. You haven't supplied that. Lastly, may I suggest you use 0 and 1 differently, since the array will initially be full of zeroes, not ones. – Dawood ibn Kareem Feb 14 '19 at 19:44
  • And to solve your real problem: start by researching how that sieve of E works. There are many tutorials and websites out there explaining what it is, and how it works. And from there, simply compute that algorithm yourself for a few numbers. Then try to turn your knowledge into code. – GhostCat Feb 14 '19 at 19:45

1 Answers1

1

Give it a shot:

public static void main(String[] args) {
        byte[] marcado = new byte[1000];
        for (int i = 2; i*i < 1000; i++) {
            if (marcado[i] == 0) {
                for (int j = 2; i * j < 1000; j++) {
                    marcado[i * j] = 1;
                }
            }
        }

        // print the numbers:
        for (int i = 1; i < 1000; i++) {
            if(0 == marcado[i]){
               System.out.print(" " + i);
            }
        }
    }

Besides removing syntax errors, I reversed the logic such that marcado[i]==0 indicates a prime and noneprime otherwise.

The other possibility based on your aproach would be to initialize all the array elements with "1"(eg. with fill);

kai
  • 894
  • 1
  • 7
  • 15