From caf1eab0d699b5a94ddc0082db0f03e8181af806 Mon Sep 17 00:00:00 2001 From: skylarmt Date: Fri, 10 Apr 2015 13:16:19 -0600 Subject: [PATCH] Add isprime(n) and disable shell input while commands are working --- src/net/apocalypselabs/symat/Functions.java | 67 +++++++++++++++++-- src/net/apocalypselabs/symat/Interpreter.java | 11 ++- 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/src/net/apocalypselabs/symat/Functions.java b/src/net/apocalypselabs/symat/Functions.java index b1cd57a..7b36a2e 100644 --- a/src/net/apocalypselabs/symat/Functions.java +++ b/src/net/apocalypselabs/symat/Functions.java @@ -98,11 +98,12 @@ public class Functions { public void notify(Object message) { JOptionPane.showInternalMessageDialog(Main.mainPane, message.toString()); } - + /** * Display message dialog. - * + * * This is an alias to help JavaScript programmers. + * * @param message The message */ public void alert(Object message) { @@ -118,10 +119,11 @@ public class Functions { public String ask(String question) { return JOptionPane.showInternalInputDialog(Main.mainPane, question); } - + /** * Pause execution for the specified number of milliseconds. - * @param millis + * + * @param millis */ public void pause(long millis) { try { @@ -130,10 +132,10 @@ public class Functions { // Nothing to do. } } - + /** * @see pause() - * @param millis + * @param millis */ public void sleep(long millis) { pause(millis); @@ -445,6 +447,59 @@ public class Functions { return a; } + public boolean isprime(long n) { + int i = 2; + while (i <= sqrt(n)) { + if (n % i == 0) { + return false; + } + i++; + } + return true; + } + + public boolean isprime(String nn) { + BigInteger n = new BigInteger(nn); + BigInteger i = new BigInteger("2"); + BigInteger ns = bigIntSqRootCeil(n); + while (i.compareTo(ns) <= 0) { + if (n.mod(i).toString().equals("0")) { + return false; + } + i = i.add(BigInteger.ONE); + } + return true; + } + + /** + * Thanks to http://stackoverflow.com/a/11962756/2534036 + * + * @param x + * @return + * @throws IllegalArgumentException + */ + private BigInteger bigIntSqRootCeil(BigInteger x) throws IllegalArgumentException { + if (x.compareTo(BigInteger.ZERO) < 0) { + throw new IllegalArgumentException("Negative argument."); + } + // square roots of 0 and 1 are trivial and + // y == 0 will cause a divide-by-zero exception + if (x == BigInteger.ZERO || x == BigInteger.ONE) { + return x; + } // end if + BigInteger two = BigInteger.valueOf(2L); + BigInteger y; + // starting with y = x / 2 avoids magnitude issues with x squared + for (y = x.divide(two); y.compareTo(x.divide(y)) > 0; y = ((x.divide(y)).add(y)).divide(two)) { + } + if (x.compareTo(y.multiply(y)) == 0) { + return y; + } else { + return y.add(BigInteger.ONE); + } + + } // end bigIntSqRootCeil + /** * Get all unique permutations of the given array. * diff --git a/src/net/apocalypselabs/symat/Interpreter.java b/src/net/apocalypselabs/symat/Interpreter.java index b3c702e..8eb51d3 100644 --- a/src/net/apocalypselabs/symat/Interpreter.java +++ b/src/net/apocalypselabs/symat/Interpreter.java @@ -45,7 +45,6 @@ */ package net.apocalypselabs.symat; -import java.awt.Color; import java.awt.Font; import java.awt.event.KeyEvent; import java.io.IOException; @@ -437,6 +436,8 @@ public class Interpreter extends javax.swing.JInternalFrame { String code = inputBox.getText(); commandsForExport += code + "\n"; mainBox.append(" " + code + "\n"); + runBtn.setEnabled(false); + inputBox.setEnabled(false); new EvalThread(code).start(); } @@ -492,6 +493,14 @@ public class Interpreter extends javax.swing.JInternalFrame { history[0] = code; clrInput(); historyIndex = -1; + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + inputBox.setEnabled(true); + runBtn.setEnabled(true); + inputBox.requestFocusInWindow(); + } + }); } private void clrInput() {