Initial commit
This commit is contained in:
6
Java/Makefile
Normal file
6
Java/Makefile
Normal file
@@ -0,0 +1,6 @@
|
||||
compile:
|
||||
javac code_cycle.java
|
||||
javac code_unoptimised.java
|
||||
|
||||
clean:
|
||||
rm *.class
|
||||
20
Java/code_cycle.java
Normal file
20
Java/code_cycle.java
Normal file
@@ -0,0 +1,20 @@
|
||||
public class code_cycle {
|
||||
public static void main(String[] args) {
|
||||
int total_attempts = 256*256*256;
|
||||
int successful_attempts = 0;
|
||||
|
||||
for (int a = 0; a < 256; a++) {
|
||||
for (int b = 0; b < 256; b++) {
|
||||
for (int c = 0; c < 256; c++) {
|
||||
if (a + b + c > 300) {
|
||||
successful_attempts++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Iterations: " + total_attempts);
|
||||
System.out.println("Valid sums: " + successful_attempts);
|
||||
System.out.println("Probability: " + (float) successful_attempts / total_attempts);
|
||||
}
|
||||
}
|
||||
22
Java/code_unoptimised.java
Normal file
22
Java/code_unoptimised.java
Normal file
@@ -0,0 +1,22 @@
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class code_unoptimised {
|
||||
public static void main(String[] args) {
|
||||
int total_attempts = 15_000_000;
|
||||
int successful_attempts = 0;
|
||||
|
||||
for (int i = 0; i < total_attempts; i++) {
|
||||
int a = ThreadLocalRandom.current().nextInt(256);
|
||||
int b = ThreadLocalRandom.current().nextInt(256);
|
||||
int c = ThreadLocalRandom.current().nextInt(256);
|
||||
|
||||
if (a + b + c > 300) {
|
||||
successful_attempts++;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Iterations: " + total_attempts);
|
||||
System.out.println("Valid sums: " + successful_attempts);
|
||||
System.out.println("Probability: " + (float) successful_attempts / total_attempts);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user