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:

  1. the JVM was so compelling that nothing else mattered, or
  2. 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);
  }
}

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026

Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Primes Benchmark Source

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

Primes Benchmark Results

Other Java Versions

Java 1.0

Java 1.2

Java 1.5