import java.awt.*; import java.applet.*; import java.awt.event.*; import java.math.BigInteger; public class Goldbachia extends Applet implements Runnable { static BigInteger zero = new BigInteger("0"); static BigInteger one = new BigInteger("1"); static BigInteger two = new BigInteger("2"); private Thread kicker; Controls controls; Algorithm algorithm; private String input2; private String output2; public void init() { controls = new Controls(); setLayout(new BorderLayout()); add("Center", controls); controls.setEnabled(true); } public void run() { try { if (algorithm == null) { algorithm = new Algorithm(); } output2 = "Calculating, please wait..."; controls.ta.setText(output2); algorithm.init(); input2 = controls.tf.getText(); algorithm.input(input2); algorithm.calculate(); output2 = algorithm.output(); controls.ta.setText(output2); do { BigInteger n = new BigInteger(input2); BigInteger nplusone = new BigInteger("0"); nplusone = n.add(one); input2 = nplusone.toString(); algorithm.input(input2); algorithm.calculate(); output2 = output2 + '\n' + algorithm.output(); controls.ta.setText(output2); } while (true); } catch (Exception e) { } } public synchronized void stop() { if (kicker != null) { try { kicker.stop(); } catch (IllegalThreadStateException e) { } kicker = null; } if (algorithm != null) { try { algorithm.stop(); } catch (IllegalThreadStateException e) { } } } synchronized void startCalculation() { if (kicker == null || !kicker.isAlive()) { kicker = new Thread(this); kicker.start(); } } class Controls extends Panel implements ActionListener { TextField tf = new TextField(40); Button b = new Button("Calculate"); TextArea ta = new TextArea("", 10, 60, TextArea.SCROLLBARS_VERTICAL_ONLY); public Controls() { tf.setText("Enter a positive integer, n, here"); add(tf); b.addActionListener(this); add(b); ta.setText("Tests whether n! is a counterexample to Goldbach's Conjecture"); add(ta); } public void actionPerformed(ActionEvent ae) { String label = ae.getActionCommand(); if (label.equals("Calculate")) { startCalculation(); } } } class Algorithm { private String input; private String output; protected boolean stopRequested = false; BigInteger bestyet = new BigInteger("0"); public void init() { stopRequested = false; } public void stop() { stopRequested = true; } public void input(String strInput) { input = strInput; } public String output() { return output; } public void calculate() { BigInteger n = new BigInteger(input); boolean found = false; BigInteger test = new BigInteger("0"); test = factorial(n); BigInteger halftest = new BigInteger("0"); halftest = test.divide(two); BigInteger p = new BigInteger("0"); p = n; while ((p.compareTo(halftest) <= 0) && (found == false)) { if (isprime(p)) { BigInteger q = new BigInteger("0"); q = test.subtract(p); if (isprime(q)) { if (p.divide(n).compareTo(bestyet) > 0) bestyet = p.divide(n); output = p.toString() + " + ? = " + n.toString() + "!" + '\t' + bestyet.toString(); found = true; } } p = p.add(one); controls.ta.setText(output2 + '\n' + p.toString()); if (stopRequested) { return; } } if (found == false) { output = n.toString() + "! is a COUNTEREXAMPLE!"; } } private BigInteger factorial(BigInteger in) { BigInteger i = new BigInteger("1"); BigInteger out = new BigInteger("1"); while (i.compareTo(in) <= 0) { out = out.multiply(i); i = i.add(one); } return out; } private boolean isprime(BigInteger n) { return n.isProbablePrime(100); } } }