/** Copyright Henry Bottomley February 2002*/ import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.lang.Math; import java.math.BigInteger; public class magicapplet extends Applet implements ActionListener {TextField box0, messagebox; int maxx; public void init() { box0 = new TextField(4); add (box0); Button calculate = new Button("Calculate magic series"); calculate.addActionListener(this); add (calculate); messagebox = new TextField(50); add (messagebox); } public void actionPerformed(ActionEvent event) {String arg=event.getActionCommand(); if (arg=="Calculate magic series") { maxx=Integer.parseInt(box0.getText().trim()); int maxz=maxx*(maxx-1); int maxy=maxx*maxx*(maxx-1)/2; int numx=1; BigInteger rArray[][] = new BigInteger[maxx+1][maxy+1]; // this is large and growing and a potential memory problem for (int x=0; x<=maxx; x++) {for (int y=maxy; y>=0; y--) {rArray[x][y]=BigInteger.valueOf(0l); //start with all zero, except ... } } rArray[0][0]=BigInteger.valueOf(1l); //... except for a single one. for (int z=0; z<=maxz; z++) {for (int x=1; x<=maxx; x++) //Need to increase to use previous values {for (int y=maxy; y>=0; y--) //Need to decrease to use previous values {if (y>=x) {rArray[x][y]=rArray[x-1][y].add(rArray[x][y-x]); //the recurrence: rArray[x-1][y] is new while rArray[x][y-x] is old } else {rArray[x][y]=rArray[x-1][y]; // rArray[x][y-x] does not exist if x>y } } } if (numx*(numx-1)<=z) {messagebox.setText(Integer.toString(numx) + " " + rArray[numx][numx*numx*(numx-1)/2].toString()); numx++; } // end of if/messagebox } // end of z loop } // end of calculate magic series } // end of actionPerormed } // end of class magicapplet