/* Pi Spigot from April '95 Math Monthly ---------------------- The program basically evaluates n terms of a series for pi by starting with the last term and multiplying by 10 and carrying the remainder to the next term, reading off the ones digit, and repeating until all numerators are zeroed. Check out the issue for a more detailed explanation. Improved output by Robert Simms, of Salsibury State U (MD) and Clemson U (SC) Language: Java (manually converted from C, manually converted from Pascal) */ import java.awt.event.*; import java.awt.*; import java.applet.*; public class piRadix2 extends Applet implements ActionListener { TextField digitsField, widthField, heightField; Button calcButton, clearButton, setWidthButton, setHeightButton; TextArea out; private int n = 1000; private int blockWidth = 50, minBlockWidth = 1, maxBlockWidth = 50; private int blockHeight = 10; private int blockSpan = blockWidth * blockHeight; private int chunkSize = 5; public static void main(String[] args) { //this is for terminal output (text-only) piRadix2 worker = new piRadix2(); if(args.length>=1) { worker.setCount( Integer.valueOf(args[0]).intValue() ); } else { //worker.setCount( 100 ); } worker.calc(); if(worker.blockSpan !=0 && worker.n %worker.blockSpan !=0) { System.out.println(); } if(worker.blockWidth !=0 && worker.n %worker.blockWidth !=0) { System.out.println(); } } public void init() { setLayout(new BorderLayout()); GridBagLayout gbl1 = new GridBagLayout(); GridBagConstraints gbc1 = new GridBagConstraints(); Panel panel1 = new Panel(); panel1.setLayout(gbl1); Label digLabel = new Label("Number of decimal digits? "); gbc1.gridx = 0; gbc1.gridy = 0; gbc1.insets = new Insets(4,0,4,0); gbl1.setConstraints(digLabel, gbc1); panel1.add(digLabel); digitsField = new TextField(Integer.toString(n), 7); gbc1.gridx = 1; gbc1.gridy = 0; gbc1.insets = new Insets(0,3,0,10); gbc1.fill = GridBagConstraints.NONE; gbl1.setConstraints(digitsField, gbc1); panel1.add(digitsField); calcButton = new Button("Calculate!"); gbc1.gridx = 2; gbc1.gridy = 0; gbc1.insets = new Insets(4,0,4,0); gbl1.setConstraints(calcButton, gbc1); panel1.add(calcButton); calcButton.addActionListener(this); add(panel1, BorderLayout.NORTH); //====== end panel 1 out = new TextArea(25, maxBlockWidth + 10); add(out, BorderLayout.CENTER); //====== begin panel 2 Panel panel2 = new Panel(); GridBagLayout gbl2 = new GridBagLayout(); GridBagConstraints gbc2 = new GridBagConstraints(); panel2.setLayout(gbl2); gbc2.weighty = 1.0; Label widthLabel0 = new Label("Block Width: "); gbc2.gridy = 0; gbc2.gridx = 0; gbc2.anchor = GridBagConstraints.EAST; gbc2.weightx = 1.0; gbl2.setConstraints(widthLabel0, gbc2); panel2.add(widthLabel0); Label widthLabel1 = new Label(minBlockWidth +" <="); gbc2.gridy = 0; gbc2.gridx = 1; gbc2.anchor = GridBagConstraints.EAST; gbc2.weightx = .5; gbl2.setConstraints(widthLabel1, gbc2); panel2.add(widthLabel1); widthField = new TextField(Integer.toString(blockWidth), 6); gbc2.gridx = 2; gbc2.weightx = 0; gbc2.fill = GridBagConstraints.HORIZONTAL; gbc2.insets = new Insets(0,3,0,3); gbl2.setConstraints(widthField, gbc2); panel2.add(widthField); Label widthLabel2 = new Label("<= " +maxBlockWidth +" "); gbc2.gridx = 3; gbc2.weightx = .5; gbc2.insets = new Insets(0,0,0,0); gbc2.anchor = GridBagConstraints.WEST; gbl2.setConstraints(widthLabel2, gbc2); panel2.add(widthLabel2); setWidthButton = new Button("Set Block Width"); gbc2.gridx = 4; gbc2.weightx = 1.0; gbc2.fill = GridBagConstraints.NONE; gbc2.anchor = GridBagConstraints.CENTER; gbl2.setConstraints(setWidthButton, gbc2); panel2.add(setWidthButton); setWidthButton.addActionListener(this); Label heightLabel0 = new Label("Block Height: "); gbc2.gridy = 1; gbc2.gridx = 0; gbc2.weightx = 1.0; gbc2.fill = GridBagConstraints.NONE; gbc2.anchor = GridBagConstraints.EAST; gbl2.setConstraints(heightLabel0, gbc2); panel2.add(heightLabel0); heightField = new TextField(Integer.toString(blockHeight), 6); gbc2.gridx = 2; gbc2.weightx = 0; gbc2.fill = GridBagConstraints.NONE; gbc2.insets = new Insets(0,3,0,3); gbl2.setConstraints(heightField, gbc2); panel2.add(heightField); Label heightLabel = new Label("(use 0 for no line skip)"); gbc2.gridy = 1; gbc2.gridx = 3; gbc2.weightx = .5; gbc2.insets = new Insets(0,0,0,0); gbc2.anchor = GridBagConstraints.WEST; gbl2.setConstraints(heightLabel, gbc2); panel2.add(heightLabel); setHeightButton = new Button("Set Block Height"); gbc2.gridx = 4; gbc2.weightx = 1.0; gbc2.fill = GridBagConstraints.NONE; gbc2.anchor = GridBagConstraints.CENTER; gbl2.setConstraints(setHeightButton, gbc2); panel2.add(setHeightButton); setHeightButton.addActionListener(this); add(panel2, BorderLayout.SOUTH); this.clearOut(); this.calc(); } public void clearOut() { out.setText(""); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("Calculate!")) { System.out.println("Calculate button pressed"); setCount( Integer.valueOf(digitsField.getText()).intValue() ); /* int n = Integer.parseInt(digitsField.getText());*/ this.clearOut(); this.calc(); } else if(e.getActionCommand().equals("Set Block Height")) { setBlockHeight( Integer.valueOf(heightField.getText()).intValue() ); heightField.setText(Integer.toString(blockHeight)); } else if(e.getActionCommand().equals("Set Block Width")) { setBlockWidth( Integer.valueOf(widthField.getText()).intValue() ); widthField.setText(Integer.toString(blockWidth)); } } public void setBlockWidth(int w) { if(w < minBlockWidth) { w = minBlockWidth; } else if(w > maxBlockWidth) { w = maxBlockWidth; } this.blockWidth = w; this.updateBlockSpan(); } public void setBlockHeight(int h) { if(h < 0) { h = 0; } this.blockHeight = h; this.updateBlockSpan(); } private void updateBlockSpan() { this.blockSpan = this.blockWidth *this.blockHeight; } public void setCount(int j) { this.n = j; } private void digout(int count, int digit) { if(count > this.n) { return; } String s=""; s = s +digit; if(blockSpan != 0 && count %blockSpan == 0) { if(count == 0) { s = s +"\n"; } else { s = s +" (" +count +")\n"; } } if(blockWidth !=0 && count %blockWidth ==0) { s = s +"\n"; } else if(chunkSize !=0 && count %chunkSize ==0) { s = s +" "; } if(out==null) { System.out.print(s); } else { this.out.append(s); } } public void calc() { int count, extra=3, i, j, k, len, nines, predigit, q, x; len = (int) 10*(n+extra)/3; int[] a = new int[len]; count = -2; predigit = 0; nines = 0; for(j=0; j=0; i--) { x = 10*(a[i]) +q*(i+1); a[i] = x % (2*i+1); q = (int) x/(2*i+1); } a[0] = q % 10; q = (int) q/10; switch(q) { case 10: digout(++count, predigit +1); for(k=0; k