Friday Oct 19, 2007

Prime Twins in Java

From an algorithm by Shimrat, University of Alberta, Alberta, CA, published in "Collected Algorithms of the ACM, Volume II", this is my interpretation of the "PrimeTwins" in Java:
import java.util.Enumeration;
import java.util.Vector;

/\*\*
 \* Models an event generated by the construction of
 \* prime twins
 \*/
class PrimeTwinsEvent {

    /\*\*
     \* creates a PrimeTwinsEvent object.
     \*
     \* @param serial the serial number (sequence) of twins
     \* @param twin1 the lower twin
     \* @param twin2 the higher twin
     \*/
    public PrimeTwinsEvent(int serial,int twin1,int twin2) {
	this.serial = serial;
	this.twin1 = twin1;
	this.twin2 = twin2;
    }

    // accessors
    public int getSerial() { return this.serial; }
    public int getTwin1() { return this.twin1; }
    public int getTwin2() { return this.twin2; }

    /\*\*
     \* Prints an String interpretation of
     \* this object
     \*/
    public String toString() {
	return "serial: "
	    + serial
	    + " twin1: "
	    + twin1
	    + " twin2: "
	    + twin2;
    }

    private int serial;
    private int twin1;
    private int twin2;

}

/\*\*
 \* Classes wishing to be notifed of PrimeTwinsEvents
 \* must implement this interface
 \*/
interface PrimeTwinsListener {
    public void action(PrimeTwinsEvent event);
}

/\*\*
 \* From an algorithm by M. Shimrat, University of Alberta, Alberta, Canada,
 \* and published in the "Collected Algorithms of the ACM, Volume II", a
 \* PrimeTwins object has a generate method
 \* that generates successive prime numbers separated by two ("prime twins")
 \* and notifies listeners, if any, of the prime twins and a sequential
 \* serial number.
 \*
 \* The Java is original, but the algorithm is from the author named above.
 \*
 \* @author Terry J. Gardner
 \*/
public class PrimeTwins {

    /\*\*
     \* add a listener to be notfied. if l is null,
     \* then no action is taken, and no exception is thrown.
     \*
     \* @param l the listener to be notfied
     \*/
    public void addPrimeTwinsListener(PrimeTwinsListener l) {
	if(l == null) return;
	listeners.add(l);
    }

    /\*\*
     \* notify all configured listeners
     \*
     \* @param event the event
     \*/
    public void notifyAllListeners(PrimeTwinsEvent event) {
	Enumeration e = listeners.elements();
	while(e.hasMoreElements()) {
	    PrimeTwinsListener l = e.nextElement();
	    l.action(event);
	}
    }

    /\*\*
     \* generates successive prime twins (primes differing by two)
     \* and notifies all PrimeTwinsListeners.
     \*
     \* @param MAX maximum number of twins
     \* @throws IllegalArgumentException if max is not positive
     \*/
    public void generate(final int MAX) throws IllegalArgumentException {
	if(MAX <= 0) {
	    throw new IllegalArgumentException("max must be positive");
	}
	int j,m,previous,current,t;
	int[] p = new int[MAX+1];
	p[1] = j = 2;
	p[2] = previous = 3;
	t = 0;
	for(current = 5; current < p[j] \* p[j]; current += 2) {
	    m = 1;
	    int mult = p[m] \* p[m];
	    while(mult <= current) {
		if(current == current/mult) return;
		if(j < MAX) {
		    p[++j] = current;
		}
		if(current == previous + 2) {
		    ++t;
		    notifyAllListeners(new PrimeTwinsEvent(t,previous,current));
		}
		previous = current;
		++m;
		mult = p[m] \* p[m];
	    }
	}
    }

    // listeners
    private Vector listeners = new Vector();
    
}

No-one could claim that Java is perfect, but it is easy to use to realize algorithms.
About

Sun, LDAP, SLAMD, DSLA, java, Struts, networking, chess, books, cooking, wine, and many other things.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today