Add isprime(n) and disable shell input while commands are working
This commit is contained in:
parent
7a6eeadefc
commit
caf1eab0d6
@ -98,11 +98,12 @@ public class Functions {
|
|||||||
public void notify(Object message) {
|
public void notify(Object message) {
|
||||||
JOptionPane.showInternalMessageDialog(Main.mainPane, message.toString());
|
JOptionPane.showInternalMessageDialog(Main.mainPane, message.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Display message dialog.
|
* Display message dialog.
|
||||||
*
|
*
|
||||||
* This is an alias to help JavaScript programmers.
|
* This is an alias to help JavaScript programmers.
|
||||||
|
*
|
||||||
* @param message The message
|
* @param message The message
|
||||||
*/
|
*/
|
||||||
public void alert(Object message) {
|
public void alert(Object message) {
|
||||||
@ -118,10 +119,11 @@ public class Functions {
|
|||||||
public String ask(String question) {
|
public String ask(String question) {
|
||||||
return JOptionPane.showInternalInputDialog(Main.mainPane, question);
|
return JOptionPane.showInternalInputDialog(Main.mainPane, question);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pause execution for the specified number of milliseconds.
|
* Pause execution for the specified number of milliseconds.
|
||||||
* @param millis
|
*
|
||||||
|
* @param millis
|
||||||
*/
|
*/
|
||||||
public void pause(long millis) {
|
public void pause(long millis) {
|
||||||
try {
|
try {
|
||||||
@ -130,10 +132,10 @@ public class Functions {
|
|||||||
// Nothing to do.
|
// Nothing to do.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see pause()
|
* @see pause()
|
||||||
* @param millis
|
* @param millis
|
||||||
*/
|
*/
|
||||||
public void sleep(long millis) {
|
public void sleep(long millis) {
|
||||||
pause(millis);
|
pause(millis);
|
||||||
@ -445,6 +447,59 @@ public class Functions {
|
|||||||
return a;
|
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.
|
* Get all unique permutations of the given array.
|
||||||
*
|
*
|
||||||
|
@ -45,7 +45,6 @@
|
|||||||
*/
|
*/
|
||||||
package net.apocalypselabs.symat;
|
package net.apocalypselabs.symat;
|
||||||
|
|
||||||
import java.awt.Color;
|
|
||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -437,6 +436,8 @@ public class Interpreter extends javax.swing.JInternalFrame {
|
|||||||
String code = inputBox.getText();
|
String code = inputBox.getText();
|
||||||
commandsForExport += code + "\n";
|
commandsForExport += code + "\n";
|
||||||
mainBox.append(" " + code + "\n");
|
mainBox.append(" " + code + "\n");
|
||||||
|
runBtn.setEnabled(false);
|
||||||
|
inputBox.setEnabled(false);
|
||||||
new EvalThread(code).start();
|
new EvalThread(code).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -492,6 +493,14 @@ public class Interpreter extends javax.swing.JInternalFrame {
|
|||||||
history[0] = code;
|
history[0] = code;
|
||||||
clrInput();
|
clrInput();
|
||||||
historyIndex = -1;
|
historyIndex = -1;
|
||||||
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
inputBox.setEnabled(true);
|
||||||
|
runBtn.setEnabled(true);
|
||||||
|
inputBox.requestFocusInWindow();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void clrInput() {
|
private void clrInput() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user