Skip to content

Instantly share code, notes, and snippets.

@ababup1192
Last active May 14, 2020 00:50
Show Gist options
  • Save ababup1192/cb7883b01307ea7c09573e054b97a329 to your computer and use it in GitHub Desktop.
Save ababup1192/cb7883b01307ea7c09573e054b97a329 to your computer and use it in GitHub Desktop.
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
// Collatz.check() checks the Collatz conjecture for a given
// number n.
// Now, to prove the conjecture to be correct, we would need to
// to verify it for all n.
// This is not possible to check for all n with a given computer
// but at least we can check for several values.
//
public class CheckCollatz {
private final static BigInteger ONE = new BigInteger("1");
// Check the conjecture for 1 <= i <= n
public static boolean check(BigInteger n) {
// TODO complete
int core = Runtime.getRuntime().availableProcessors();
BigInteger s = n.divide(new BigInteger(String.valueOf(core)));
ArrayList<CalcCheckCollatz> threadList = new ArrayList<>();
for (int i = 0; i < core; i++) {
CalcCheckCollatz calcCheckCollatz = new CalcCheckCollatz(new BigInteger(String.valueOf(i)).multiply(s),
new BigInteger(String.valueOf(i)).multiply(s).add(s).subtract(ONE));
calcCheckCollatz.start();
threadList.add(calcCheckCollatz);
}
for (CalcCheckCollatz calcCheckCollatz : threadList) {
try {
calcCheckCollatz.join();
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage:");
System.out.println("java CheckCollatz n");
System.out.println("will check the Collatz conjecture for");
System.out.println("every 1 <= i <= n");
System.exit(1);
}
long start_time = new Date().getTime();
boolean v = CheckCollatz.check(new BigInteger(args[0]));
long end_time = new Date().getTime();
System.out.println("Ellapsed time: " + (end_time - start_time) + "ms");
if (v) {
System.out.println("The conjecture seems valid up to n=" + args[0]);
} else {
System.out.println("The conjecture is not valid");
}
}
}
class CalcCheckCollatz extends Thread {
private BigInteger lower;
private BigInteger upper;
CalcCheckCollatz(BigInteger lower, BigInteger upper) {
this.lower = lower;
this.upper = upper;
}
@Override
public void run() {
while (upper.compareTo(lower) > 0) {
boolean result = Collatz.judgmentResult(upper);
if (!result) {
return;
}
upper = upper.subtract(BigInteger.ONE);
}
}
}
public class ThreadSample {
public static void main(String[] args) {
int core = Runtime.getRuntime().availableProcessors();
int n = core * 10;
int s = n / core;
System.out.println(core);
System.out.println(n);
System.out.println(s);
for (int i = 0; i < core; i++) {
new CalcCollatz(i * s, i * s + s - 1).start();
}
}
}
class CalcCollatz extends Thread {
private int lower;
private int uppper;
CalcCollatz(int lower, int upper) {
this.lower = lower;
this.uppper = upper;
}
@Override
public void run() {
System.out.println("lower = " + lower + " upper = " + uppper);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment