1

I have the following code for a primes program:

class Test2 {
    public static void main(String[] args) {
        System.out.println("Prime numbers inbetween 2-100: ");
        boolean isComposite = false;
        for (int i = 2; i <= 100; i++) {
            if ((i % 2) == 0) {
                continue;
            }

            for (int k = 3; k < i; k++) {
                if ((i % k) == 0) {
                    isComposite = true;
                    break;
                }
            }

            if (!isComposite) {
                System.out.println(i);
                isComposite = false;
            }
        }//End for
    }//End main()
}//End class

My problem is, when I run that code I get the following output:

Prime numbers inbetween 2-100:
3
5
7

Such simple code, but I can't figure out whats wrong with it! Any help would be appreciated.

Also, what is the best algorithm for finds primes in Java?

RobertR
  • 745
  • 9
  • 27
  • Make sure you include 2 as a prime number. – pushkin Oct 19 '15 at 06:03
  • 2
    you could also just print `2` as primnumber and start with `i = 3` and increment `i` by 2, so you skip every even number – SomeJavaGuy Oct 19 '15 at 06:03
  • @Kevin Esche Good idea! Never thought of that will do. – RobertR Oct 19 '15 at 06:04
  • 1
    @RobertR another improvement you could implement is your second for loop. It just needs to loop until `k < i/2`. – SomeJavaGuy Oct 19 '15 at 06:08
  • Your algorithm is very inefficient, I'd recommend looking up and implementing the Sieve of Eratosthenes algorithm. – James Oct 19 '15 at 06:13
  • @RobertR Is there a good reason for treating 2 as a special case? Why not start i and k at 2 in their loops? Another useful change (other than just using a Sieve of Eratosthenes) would be to only search while k*k <= i, if you haven't a factor by then, you won't find any. This one isn't so critical as making 2 non-special though. – moreON Oct 19 '15 at 06:28
  • @James Zafar I just looked it up. If I understand correctly you simply sift out all the multiples of 2, 3, 5, 7 (which are primes them selfs), and then the remaining are all primes? – RobertR Oct 19 '15 at 15:48

2 Answers2

4

You forgot to reset isComposite, so after 9 is detected as composite, all the following numbers are considered composite by mistake :

   for (int i = 2; i <= 100; i++) {
        isComposite = false; // add this
        ...

P.S, 2 is also a prime number which your code misses, so you should print it separately:

    System.out.println(2);
    boolean isComposite = false;
    for (int i = 3; i <= 100; i++) {
        isComposite = false;
        if ((i % 2) == 0) {
            continue;
        }

        for (int k = 3; k < i; k++) {
            if ((i % k) == 0) {
                isComposite = true;
                break;
            }
        }

        if (!isComposite) {
            System.out.println(i);
            isComposite = false;
        }
    }//End for
Eran
  • 387,369
  • 54
  • 702
  • 768
4

You need to reset the flag isComposite in loop as:

for (int i = 2; i <= 100; i++) {
            isComposite = false;
            if ((i % 2) == 0) {
                continue;
            }

            for (int k = 3; k < i; k++) {
                if ((i % k) == 0) {
                    isComposite = true;
                    break;
                }
            }

            if (!isComposite) {
                System.out.println(i);
                isComposite = false;
            }
        }

Else it will remain true forever in loop. Now it prints:

Prime numbers inbetween 2-100: 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

akhil_mittal
  • 23,309
  • 7
  • 96
  • 95