I am supposed to make a class PrimeNumberGenerator which has a method nextPrime that will print out all prime numbers up to a number the user input
I wrote this function which lists the first n prime numbers:
static void primes(int n) {
int[] prime = new int[n];
prime[0] = 2;
int pp = 0;
int i = 2;
while(pp < n - 1) {
int g = 1;
for(int p = 0; p <= pp; p++) {
if(i % prime[p] == 0) {
g = prime[p];
break;
}
}
if(g == 1) {
pp += 1;
prime[pp] = i;
}
i += 1;
}
for(int z = 0; z < n; z++) {
System.out.println(prime[z]);
}
}
It also should be relatively fast because it checks the lowest amount of divsors necessary. (i think)