Ok, upon OP's request I'm sending my optimized java code.
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
public class TriangleChecker {
// We are going to use 1 thread per core. MacMini with M4 silicon has 10 cores
public static final int THREAD_COUNT = Runtime.getRuntime().availableProcessors();
// Intermediate test results will be reported after REPORT_BATCH number of tests
public static final long REPORT_BATCH = 1_000_000L;
// Total number of tests that we are going to run;
public static final long TEST_COUNT = 10_000_000_000L;
// Increase RND_TRESHOLD if you want to change random number generator more often
// Keep it small - if it is too big, it will impact performance
public static final double RND_TRESHOLD = 0.00000001;
// TASK_QUEUE_SIZE defines the maximum number of queued tests,
// you don't need to change it, making it bigger will *not* make the code faster
public static final int TASK_QUEUE_SIZE = 100;
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor =
new ThreadPoolExecutor(THREAD_COUNT, THREAD_COUNT, 0, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(TASK_QUEUE_SIZE));
// This handler is triggered when the task queue is full
threadPoolExecutor.setRejectedExecutionHandler((task, executor) -> {
try {
// Let's submit the task again. The pool is using blocking queue.
// So main thread will wait until place in the queue becomes available
executor.getQueue().put(task);
}
catch(InterruptedException ie) {
System.err.println("Interrupted while submitting a new test: " + ie.toString());
}
});
System.out.println(
String.format("Created thread pool with %d threads", THREAD_COUNT)
);
AtomicLong okCount = new AtomicLong(0), totalCount = new AtomicLong(0);
while(totalCount.get() < TEST_COUNT) {
threadPoolExecutor.submit(() -> {
if(new Triangle().isOk()) {
okCount.incrementAndGet();
}
long total = totalCount.incrementAndGet();
if(total % REPORT_BATCH == 0) {
double pct = 100 * (double) okCount.get() / (double) total;
System.out.println(
String.format("Current percentage: %.8f (after %,d runs)", pct, total)
);
}
});
}
threadPoolExecutor.close();
}
}
class Triangle {
// WLOG, we can assume that R=1
private final double a, b, c, r, area;
public Triangle() {
// Triangle has 3 random points on a unit circle
Point pA = new Point(), pB = new Point(), pC = new Point();
double d1 = pA.distance(pB), d2 = pB.distance(pC), d3 = pC.distance(pA);
double ccf = d1 + d2 + d3;
a = Math.min(d1, Math.min(d2, d3));
c = Math.max(d1, Math.max(d2, d3));
b = ccf - a - c;
double s = ccf / 2D;
// Heron's formula
area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
r = area / s;
}
// This is the test that we are making for every random triangle
public boolean isOk() {
return c + b - a > 1D + r + Math.sqrt(area);
}
}
// Random point on a unit circle
class Point {
private static Random random = new Random();
private static final double fullCircle = 2D * Math.PI;
private final double angle = random.nextDouble(fullCircle);
private final double x = Math.cos(angle), y = Math.sin(angle);
Point() {
if(random.nextDouble() < TriangleChecker.RND_TRESHOLD) {
System.out.println("--- Changing random number generator ---");
random = new Random();
}
}
double distance(Point p) {
double dx = x - p.x, dy = y - p.y;
return Math.sqrt(dx * dx + dy * dy);
}
}
Just put the code in a file named TriangleChecker.java. Adjust the program parameters (THREAD_COUNT, REPORT_BATCH, TEST_COUNT, RND_TRESHOLD) Then compile and run it:
javac TriangleChecker.java
java TriangleChecker
Becuase, this is Java, you can run it on every platform, on every machine imaginable. On my brand new MacMini with M4 silicon (10 cores), I was able to execute 10 billion tests in just a few (2-3) hours. Performance on my Mac laptop with M1 silicon was slightly lower so I gave up early and turned to M4. My guess is that on a comparable hardware, the program could easily complete 100 billion tests in about a day. I haven't had the opportunity to test the performance on some modern day Intel or AMD chip.
Hope this helps. If you want some adjustments, please let me know.