-4

This code is meant to find the 1001st prime but gives me the answer 47 which is clearly wrong.

public class problem7 {

   public static void main(String[] args) {
        int[] myarray = new int[1001];
        int j = 0;
        boolean prime = false;

        for (int i = 2;; i++) {
            for (int k = 2; k < i; k++) {
                if (i == (k - 1) && i % k != 0) {
                    prime = true;
                }

                if (i % k == 0) {
                    prime = false;
                    prime = true;
                }
                if (prime) {
                    myarray[j] = i;
                }
                if (j == 1000) {
                    System.out.println(myarray[1000]);
                    return;
                }

                j++;
            }

        }
    }
}

Any help would be greatly appreciated.

pschueller
  • 4,362
  • 2
  • 27
  • 50

4 Answers4

1

I think j++ is increment only if prime number is inserted not at all case.By using this code you will be get your 1001 Prime number

public static void main(String[] args) {
        int[] myarray = new int[1001];
        int j = 0;

        for (int i = 2;; i++) {
            boolean prime = false;
            for (int k = 2; k < i; k++) {
                if (i % k == 0) {
                    prime = true;
                }
            }
            if (!prime) {
                myarray[j] = i;
                j++;
            }
            if (j == 1001) {
                break;
            }
        }
        for (int primeNumber : myarray) {
            System.out.println(primeNumber);
        }
    }
Charnjeet Singh
  • 3,056
  • 6
  • 35
  • 65
1

Your check for prime is wrong: you cannot declare a number prime and set prime = true based on a single test. The inner loop should set prime to false, but it shouldn't reset it to true: once it's false, it's false.

The algorithm should proceed as follows:

  • For each i, set prime=true
  • Loop over potential divisors k
  • If a divisor such that i % k == 0 is found, set prime = false, and break the loop
  • If prime is still true at the end of the nested loop, add i to the list of primes.

This should give you a correct result, at which point you should consider optimizing your solution using considerations below:

  • If you did not find a divisor among k below or at sqrt(i), then i is prime
  • You do not have to try all numbers k, only the ones from the list of primes that you have discovered so far.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

This is an implementation of dasblinkenlights algorithm!

public static void main(String args[]) {
    int[] primes = new int[1001];
    int i = 1;
    int index = 0;
    while (primes[1000] == 0) {
        i++;
        boolean skip = false;
        for (int i1 : primes) {
            if (i1 == 0)
                break;
            if (i % i1 == 0) { //checks if the number is a multiple of previous primes, if it is then it skips it
                skip = true;
                break;
            }
        }
        if (!skip) {
            if (isPrime(i)) {
                primes[index] = i;
                System.out.println(i);
                index++;
            }
        }
    }
}

static boolean isPrime(int n) {
    for (int i = 2; i < n; i++) {
        if (n % i == 0)
            return false;
    }
    return true;
}
Roberto Anić Banić
  • 1,411
  • 10
  • 21
0

The primality test you done here is kind of ambiguous. Because the general approach is, we pick any number assuming its prime, so at the beginning, prime = true. Then if there exist any factor k of input such that k < input and kthen the number is not prime, so prime = false.

I modified your code and get a result: 104743 .

Update: Here is a bit faster way to find large prime. The inner loop will iterate up to square root of i, reason: Why do we check upto the square root of a prime number to determine if it is prime?

public static void main(String[] args) {
    int[] myarray = new int[10001];
    int j = 0;
    boolean prime = true;
    int i = 2;
    while (true) {
        prime = true;
        for (int k = 2; k*k <= i; k ++) {
            if (i % k == 0) {
                prime = false;
            }
        }
        if (prime) {
            myarray[j] = i;
            if (j == 10000) {
                System.out.println(myarray[10000]);
                return;
            }
            j++;
        }  
        if(i > 4)
            i += 2;
        else
            i++;
    }
}
Community
  • 1
  • 1
mazhar islam
  • 5,561
  • 3
  • 20
  • 41