Primes by Trial Division - Java 1.2
1 October 2017
Java prior to 1.5 lacks generics. Also, the type system still has a lot of warts around basic types (e.g. int vs. Integer), but the standard library is now complete enough it's not entirely hopeless as it was in 1.1.
// n Primes import java.util.ArrayList; import java.util.Iterator; public class nPrimes { public static void main (String[] args) { int count = 2; int numPrimes = Integer.parseInt(args[0]); ArrayList primes = new ArrayList(1); do { primes.add(new Integer(count)); count++; for (int i = 0; i < primes.size(); i++) { if (count % ((Integer)primes.get(i)).intValue() == 0) { count++; i=-1; } } } while (primes.size() < numPrimes); Iterator i = primes.iterator(); while(i.hasNext()) { System.out.println(i.next()); } } }