Prime.java

package com.bozoid.cis370.hw01.complex;

/**
Generate the first <code>n</code> prime numbers.

@author Walt Leipold
@version 1.0
*/
public class Prime
{
    /**
    Invoke this function from the command line with one argument, 
    <code>n</code>, the number of primes to generate.
    */
    public static void main(String argv[])
    {
        int nprimes;
        int a[];
        int i;

        if (argv.length != 1) {
            System.err.println(
                "Usage: Prime <number of primes to print>");
            System.exit(2);
            }
        nprimes = Integer.parseInt(argv[0]);

        a = buildArrayOfPrimes(nprimes);

        // Print primes. If more than 10, print first 5 & last 5.
        if (nprimes <= 10)
            for (i=0; i<nprimes; i++)
                System.out.println("   " + a[i]);
        else {
            for (i=0; i<5; i++)
                System.out.println("   " + a[i]);
            System.out.println("   ...");
            for (i=nprimes-5; i<nprimes; i++)
                System.out.println("   " + a[i]);
            }

        // Print summary.
        System.out.println("The " + ordinal(nprimes) + 
            " prime number is " + a[nprimes-1]);
    }

    /**
    Build and return an integer array containing the first
    <code>nprimes</code> prime numbers.  If <code>nprimes</code> is 
    not a positive integer, assumes <code>nprimes</code> is 1.

    @param nprimes the number of primes to return
    */
    public static int[] buildArrayOfPrimes(int nprimes)
    {
        if (nprimes <= 0)
            nprimes = 1;

        int primes[] = new int[nprimes];
        primes[0] = 2;

        int primecount = 1;
        int test = 3;
        while (primecount < nprimes) {
            boolean foundFactor = false;
            for (int i=0; i<primecount; i++)
                if (test % primes[i] == 0) {
                    foundFactor = true;
                    break;
                    }
            if (!foundFactor)
                primes[primecount++] = test;
            test += 2;
            }

        return primes;
    }

    /**
    Representation of an integer as an ordinal number.  Does not (and
    should not) handle negative numbers.

    @param n the number to be represented

    @return a string representing the ordinal number of <CODE>n</CODE>, 
    or &quot;<CODE>&lt;negative&gt;</CODE>&quot; for a negative number.
    */
    public static String ordinal(int n)
    {
        String lowOrdinals[] = 
            {"zeroth","first","second","third"};

        if (n < 0)
            return "<negative>";
        else if (n <= lowOrdinals.length-1)
            return lowOrdinals[n];
        else if (n <= 20)
            return n + "th";
        else {
            if (n % 10 == 1)
                return n + "st";
            else if (n % 10 == 2)
                return n + "nd";
            else if (n % 10 == 3)
                return n + "rd";
            else
                return n + "th";
            }
    }
}