I want this Java program to stream 10001 prime numbers, but it inexplicably decides to label 16 as a prime number.
The algorithm here is just keeping a running count of prime numbers, and checking each new number to see if it is divisible by any of the primes less than it. If it isn't, then it is added to the array primes[ ], the number is displayed on the console, and the process continues, until primes[ ] is full.
public static void main(String[] args){
int[] primes = new int[10001];
int primeCount = 1;
int testNumber = 3;
primes[0] = 2;
while(primeCount < 10001){
for (int i = 0; i < primeCount; i++){
if (testNumber % primes[i] == 0){
i = 0;
testNumber++;
}
}
primes[primeCount] = testNumber;
System.out.println(testNumber);
primeCount++;
testNumber++;
}
}
Console readout:
3 5 7 11 13 16 17 19 . . .
Everything else looks to be in order except for the 16... any ideas?