/* triples.java by MR_X */ /* free to distribute */ /* finds all Pythagorean triples (a,b,c) such that */ /* a^2+b^2 = c^2, where a and b are each less than MAX */ import java.applet.Applet; import java.awt.*; public class trip extends Applet { int MAX=5000; int key=5; int a,b,c, sucess=0; double d; boolean statusNum = false; int xpos=55, ypos=300, numTrials=10; int upto=1000; TextArea ta = new TextArea("Ready...",15,40); String line; StringBuffer buf = new StringBuffer(); public void init(){ setBackground(new Color(180,180,255)); Choice d= new Choice(); d.addItem("5"); d.addItem("10"); d.addItem("25"); d.addItem("50"); d.addItem("100"); d.addItem("250"); d.addItem("500"); add(new Label("Select Number of Trials: ")); add(d); add(new Button("Calculate")); add(ta); // repaint(); } public void paint(Graphics g) { if (statusNum){ int a=1,b; for (a=1;a<=MAX; a++){ for (b=a;b<=MAX; b++){ d=Math.sqrt((double)(a*a+b*b)); if (d==rint(d)) { if (prime(a,b)) { //System.out.println(a+" "+b+" "+(int)rint(d)); buf.append("("+a+" , "+b+","+(int)rint(d)+")\n"); sucess++; if (sucess==key) b=a=MAX; } } } } ta.setText(buf.toString()); buf = new StringBuffer(); //Clean off buffer sucess=0; statusNum=false; } } // Paint public boolean prime(int a, int b) { int i; double ai, bi; for (i=2;i<=a;i++){ ai=(double)a/i; bi=(double)b/i; if ((ai==rint(ai)) && (bi==rint(bi))){ return false; } } return true; } public boolean action(Event evt, Object arg) { if (evt.target instanceof Choice){ key=(Integer.parseInt((String)arg)); } if (evt.target instanceof Button){ statusNum = true; ta.setText("Ready..."); repaint(); } return true; } public int rint(int x) { return (Math.round(x)); } public double rint(double x) { return (Math.round(x)); } public double roundDouble(double x, int place) { double times=1.0; for (int j=0;j