Primes by Trial Division - Java 1.0

28 December 2017
The Java type system has so many warts around basic types (e.g. int vs. Integer) and Array types and everything else that it's a wonder it caught on at all. Two possible explanations come to mind:
- the JVM was so compelling that nothing else mattered, or
- C++ is even worse.
// n Primes
public class nPrimes {
public static void main (String[] args) {
int found = 0;
int numPrimes = Integer.parseInt(args[0]);
long count = 2;
long[] primes = new long[numPrimes];
do {
System.out.println(count);
primes[found] = count;
count++;
for (int i = 0; i < found; i++) {
if (count % primes[i] == 0) {
count++;
i=-1;
}
}
found++;
} while (found < numPrimes);
}
}