import java.io.FileNotFoundException; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Random;
public class Checks implements Runnable {
//Initialises the variables that we are going to use.
BigInteger startNum; //Starting number.
BigInteger num; //Stores the starting number.
String name, content; //Our name.
long time,c; //The time the master thread made us.
PrintWriter writer; //The file handle.
BigInteger add = BigInteger.ZERO; //How much we stagger our start by.
int list[];
int id;
int x,y;
int certainty;
BigInteger FIVE = BigInteger.valueOf(5);
public Checks(BigInteger nums, String tName, long sTime, PrintWriter fname, long con, int numThreads, int stagger, int base, int expo, int ce, int[] prime) throws FileNotFoundException {
x = base;
y = expo;
c = con;
certainty = ce;
writer = fname;
list = prime;
id = (stagger/2)+1;
startNum = nums;
num = nums;
name = tName; //Each thread is identified by its name.
add = BigInteger.valueOf(2).multiply(BigInteger.valueOf(numThreads)); //The reason for this is a bit long and complicated so it is described below.
/* We don't want the threads to analyse a number that one of the other threads has analysed.
* So we stagger their starts so they always do a number which hasn't been done before.
* However we also don't want them to analyse even numbers, so to skip the evens we multiply the stagger by 2.
*/
num = num.add(BigInteger.valueOf(stagger));
}
public void run() {
//Main loop.
time = System.currentTimeMillis()/1000;
Random r = new Random();
while(true) {
num = num.add(add); //Skips even numbers, includes our stagger.
if (!num.mod(FIVE).equals(BigInteger.ZERO)) { //Skips all numbers ending in 5.
if(factor2(num, list)) {
if(Fermat(num, r)) {
if(Prime(num, certainty)) {
content = name+x+"^"+y+"+"+(num.subtract(startNum).add(BigInteger.valueOf(c)))+" ("+getTime((System.currentTimeMillis()/1000)-time)+") "+num;
System.out.println("\n"+content);
writer.println(content);
writer.flush();
} else {
System.out.println("!");
}
} else {
//If number isn't a prime.
System.out.print((num.subtract(startNum).add(BigInteger.valueOf(c))));
}
} else {
System.out.print("-");
}
}
}
}
public static boolean factor2(BigInteger i, int[] list) {
boolean check = true;
if(i.mod(BigInteger.valueOf(2)).equals(BigInteger.ZERO)) {
return false;
}
for(int k = 0 ; k < list.length; k++) {
if(i.mod(BigInteger.valueOf(list[k])).equals(BigInteger.ZERO)) {
check = false;
break;
}
}
return check;
}
public static boolean Fermat(BigInteger p, Random r) {
BigInteger a = BigInteger.valueOf(r.nextInt(32)+2);
int check = a.modPow(p.subtract(BigInteger.ONE), p).intValue();
return (check == 1);
}
public static boolean Prime(BigInteger i, int certainty) {
boolean check = false;
if (i.isProbablePrime(certainty) == true) {
check = true;
}
return check;
}
public static String getTime(long input) {
String output = "";
if(input > 60) {
output = ((int)Math.floor(input/60))+" minute(s) "+(input%60)+" second(s)";
if(input > 3600) {
output = ((int)Math.floor((input/60)/60))+" hour(s) "+(((input%3600)-(input%60))/60)+" minute(s) "+(input%60)+" second(s)";
}
} else {
output = input+" second(s)";
}
return output;
}
}