Last active
February 26, 2022 09:42
-
-
Save joriki/9cd56577b07d870139c67e009c86dd5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Random; | |
public class Question4390194a { | |
final static Random random = new Random(); | |
final static int ntrials = 100000000; | |
final static int [] [] families = { | |
{2,4,6}, | |
{2,3,5}, | |
{1,4}, | |
}; | |
final static List<Integer> triples = new ArrayList<>(); | |
static { | |
for (int a : families [0]) | |
for (int b : families [1]) | |
if (b != a) | |
for (int c : families [2]) | |
if (c != a && c != b) | |
triples.add ((1 << a) | (1 << b) | (1 << c)); | |
} | |
public static void main(String [] args) { | |
int s = triples.size(); | |
int [] c = new int [7]; | |
for (int bits = 1;bits < 1 << s;bits++) { | |
int b = 0; | |
int sign = -1; | |
for (int i = 0;i < s;i++) | |
if ((bits & (1 << i)) != 0) { | |
b |= triples.get(i); | |
sign = -sign; | |
} | |
int count = 0; | |
for (int i = 1;i <= 6;i++) | |
if ((b & (1 << i)) != 0) | |
count++; | |
c [count] += sign; | |
} | |
for (int i = 3;i < 7;i++) | |
System.out.println(i + " : " + c [i]); | |
System.out.println(); | |
long nrolls = 0; | |
for (int n = 0;n < ntrials;n++) { | |
int seen = 0; | |
outer: | |
for (;;) { | |
int roll = random.nextInt(6); | |
nrolls++; | |
seen |= 1 << (roll + 1); | |
for (int t : triples) | |
if ((seen & t) == t) | |
break outer; | |
} | |
} | |
System.out.println(nrolls / (double) ntrials); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment