3

I'm trying to make a while loop that iterates through every long number possible and add every prime number it encounters into an the primes array. Since the while loop is supposed to run until the length of primes is 200, I expect the primes array to be filled with the first 200 prime numbers. Instead I get all zeroes. I have successfully gotten 20 rows of 10 characters each with a space in between them. How may I get them to be the actual prime numbers though?

public class PrimeGenerator {

    public static void main(String[] args) {

        long primes[] = new long[200]; 

        while (primes.length > 200){

            for(long y  = 2; y < Long.MAX_VALUE; y++) {
                int primeCounter = 0;

                if (isPrime(y) == true){
                    primes[primeCounter] = y;
                    primeCounter++;
                }
            }


        }

        for (int i = 0; i < 20; i++) {

            int primeCounter = 0;

            for(int p = 0; p < 10; p++) {
                System.out.print(primes[primeCounter] + " ");
                primeCounter++;
            }

            System.out.println();
        }
    }


    public static boolean isPrime(long number) {

        if (number % 2 == 0)
            return false;
        if (number == 2)
            return true;

        for(int x = 3; x*x <= number; x+=2) {   
            if (number%x == 0)
                return false;   

        } 

        return true;


    }

}
2
  • I'm still getting all zeroes. Commented Nov 17, 2016 at 7:05
  • @ScaryWombat That won't help either, since primes.length is remains constant (200). Commented Nov 17, 2016 at 7:05

5 Answers 5

1

primes.length is always 200 so the while loop is never entered.

The while loop is useless. Just add a condition to the for loop that would exit when the entire array has been assigned. Also move the initialization of primeCounter to be outside the for loop. Otherwise all the primes will be assigned to primes[0].

long primes[] = new long[200]; 
int primeCounter = 0;
for(long y  = 2; y < Long.MAX_VALUE && primeCounter < 200; y++) {
    if (isPrime(y) == true){
        primes[primeCounter] = y;
        primeCounter++;
    }
}
for (int i = 0; i < primes.length; i++) {
    System.out.print(primes[i]);
    if ((i+1) % 10 == 0)
        System.out.println();
}

EDIT :

As Sweeper commented, you should also fix your isPrime method, since it returns false for 2 :

public static boolean isPrime(long number) {
    if (number == 2)
        return true;
    if (number % 2 == 0)
        return false;

    for(int x = 3; x*x <= number; x+=2) {   
        if (number%x == 0)
            return false;   
    } 
    return true;
}
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't print 2 though. The OP's isPrime is wrong as well.
@Sweeper I didn't look into isPrime(). I assumed it was correct. Thanks!
The professor gave me this link when referring to the first 10,000 primes which she wanted me to get the first 200 from: primes.utm.edu/lists/small/10000.txt it shows 2 as one of the prime numbers so I set the method to accept 2 as a prime in order to satisfy the assignment requirements.
@Jason_Silva Yes, 2 is prime, but you put the if (number % 2 == 0) condition before the if (number == 2), so you return false for 2.
0

this code down

    long primes[] = new long[200]; 

    while (primes.length > 200){

means

while (200 > 200){

or the same as

while (false){

so your loop is NEVER executed!

Comments

0

because you did:

while (primes.length > 200)

and the length of the array is always 200,you never get into the while loop , and the zero in the array are coming because when you create array of "long" it initialized him with zeros

Comments

0

Firstly, the length of an array doesn't change. So, when you are testing for primes.length > 200 this will always be false, and the loop is never even entered. Therefore all values in the array are left at the default value of 0.

For doing this I would doing something like the following:

int primeCounter = 0;
long current = 0L;
while(primeCounter < primes.length){
    if(isPrime(current)){
        primes[primeCounter] = current;
        primeCounter++;
    }
    current++;
}

Comments

0

An array's length never changes. If you declared an array to have a length of 200, it will always have a length of 200. Because of this, your while loop is never executed, not even once.

There are a lot of other errors in the code, so I tried to create a solution with as few changes as possible:

public static void main(String[] args) {

    int primeCounter = 0;
    long nextPossiblePrime = 2;
    long primes[] = new long[200];

    while (primeCounter < 200) {

        for (long y = nextPossiblePrime; y < Long.MAX_VALUE; y++) {

            if (isPrime(y) == true) {
                primes[primeCounter] = y;
                primeCounter++;
                nextPossiblePrime = y + 1;
                break;
            }
        }


    }

    primeCounter = 0;
    for (int i = 0; i < 20; i++) {

        for (int p = 0; p < 10; p++) {
            System.out.print(primes[primeCounter] + " ");
            primeCounter++;
        }

        System.out.println();
    }
}


public static boolean isPrime(long number) {
    if (number == 2 || number == 3)
        return true;

    if (number % 2 == 0)
        return false;


    for (int x = 3; x * x <= number; x += 2) {
        if (number % x == 0)
            return false;

    }

    return true;


}

The first problem is that you created two primeCounters, which is not needed. I removed the extra one and moved the scope of it to the scope of the method. The next problem is that your first for loop doesn't remember the prime number that it is on and it doesn't stop when it has found one so it will keep adding the 200th prime to the array. I fixed this by adding a nextPossiblePrime variable that stores what number should the program check next. The last problem is that your isPrime method is written incorrectly. I fixed it for you!

Here's another (cleaner) solution, which still uses a while loop:

public static void main(String[] args) {
    ArrayList<Long> primes = new ArrayList<>();
    long y = 2;
    while (y < Long.MAX_VALUE && primes.size() < 200) {
        if (isPrime(y) == true){
            primes.add(y);
        }
        y++;
    }
    for (int i = 0; i < primes.size(); i++) {
        System.out.print(primes.get(i) + " ");
        if ((i+1) % 10 == 0)
            System.out.println();
    }
}


public static boolean isPrime(long number) {
    if (number == 2 || number == 3)
        return true;

    if (number % 2 == 0)
        return false;


    for (int x = 3; x * x <= number; x += 2) {
        if (number % x == 0)
            return false;

    }

    return true;


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.