/* Written by Nick Exner on 7/22/98 * * Free to distribute */ /*********************************************************** * Modified by Michael McKelvey * * 3/25/02 * * Fixed repainting issue-now it only shows the triples * * when the calculate button is pressed, not when the * * number of triples is selected from the drop-down menu * ***********************************************************/ import java.applet.Applet; import java.awt.*; public class prime extends Applet { boolean statusNum = false, showStats = false; int xpos=55, ypos=300, numTrials=10; int upto=1000; TextArea ta = new TextArea("Ready...",15,40); String line; StringBuffer buf = new StringBuffer(); int num1=0, num2=0, xpos1=5, ypos1=105, success=0, failure=0; public void init() { setBackground(new Color(180,180,255)); Choice d= new Choice(); d.addItem("10"); d.addItem("50"); d.addItem("100"); d.addItem("500"); d.addItem("1000"); d.addItem("5000"); d.addItem("10000"); d.addItem("50000"); add(new Label("Select Number of Trials: ")); add(d); add(new Button("Calculate")); add(ta); } public void paint(Graphics g) { g.setColor(Color.lightGray); g.drawRect(xpos-3,ypos-13,175,40); g.setColor(Color.white); g.fillRect(xpos-4,ypos-14,175,40); g.setColor(Color.black); g.drawRect(xpos-5,ypos-15,175,40); g.drawRect(xpos-4,ypos-14,175,40); g.drawString("Success #",xpos,ypos); g.drawString("Fail #",xpos+70,ypos); g.drawString("Ratio",xpos+120,ypos); if (statusNum) { num1=0; num2=0; xpos1=5; ypos1=105; success=0; failure=0; buf.append(numTrials + " trials:\n\n"); for(int i=1;i <= numTrials; i++) { num1=(int)(Math.random()*upto); num2=(int)(Math.random()*upto); if (tryRelative(num1,num2)) { buf.append("("+num1+" , "+num2+")*"); success++; if (i%2==0 && i>0) buf.append("\n"); else { if (num1>99 && num2 > 99) buf.append(" "); else if (num1>9 && num2 > 9) buf.append(" "); else buf.append(" "); } } else { buf.append("("+num1+" , "+num2+")"); failure++; if (i%2==0 && i>0) buf.append("\n"); else if (num1>99 && num2 > 99) buf.append(" "); else if (num1>9 && num2 > 9) buf.append(" "); else buf.append(" "); } } ta.setText(buf.toString()); buf = new StringBuffer(); statusNum = false; } // statusNum if(showStats) { g.drawString(""+success,xpos,ypos+15); g.drawString(""+failure,xpos+70,ypos+15); if (success > failure) { g.drawString(""+roundDouble((1.0*failure/success),2),xpos+120,ypos+15); g.drawString("-/+",xpos+151,ypos); } else { g.drawString(""+roundDouble((1.0*success/failure),2),xpos+120,ypos+15); g.drawString("+/-",xpos+151,ypos); } } } // Paint public boolean tryRelative(int a, int b) { for (int i=2; i<((a