Skip to content

Instantly share code, notes, and snippets.

@joriki
joriki / Question260072.java
Created December 16, 2012 21:27
Find cycles of three six-sided dice such that each beats the next one with the same maximal probability; see http://math.stackexchange.com/questions/260072.
public class Question260072 {
public static void main (String [] args) {
int [] [] dice = new int [3] [6];
int [] counter = new int [3];
int limit = (int) Math.pow (3,18);
int max = 0;
outer:
for (int selection = 0;selection < limit;selection++) {
@joriki
joriki / Question261357.java
Created December 19, 2012 00:48
Calculate probabilities of breaking even or winning at least the expected value at some point in a series of bets; see http://math.stackexchange.com/questions/261357.
public class Question261357 {
final static boolean even = false;
final static int percent = 74;
final static double p = percent / 100.;
final static int win = 7;
final static int lose = 6;
static double [] [] cache;
static boolean [] [] done;
@joriki
joriki / Question262629.java
Created December 20, 2012 13:59
Calculate the number of possible draws in the draw for the round of 16 in the 2012–13 UEFA Champions League; see http://math.stackexchange.com/questions/262629.
public class Question262629 {
final static String [] [] associations =
{
{"FRA","GER","SPA","GER","ITA","GER","SPA","ENG"},
{"POR","ENG","ITA","SPA","UKR","SPA","SCO","TUR"}
};
static int count;
static boolean [] paired = new boolean [8];
@joriki
joriki / Question264698.java
Created December 25, 2012 13:29
Calculate the average number of edges in a graph uniformly randomly selected from all acyclic digraphs on n nodes with at most one incoming edge and at most one outgoing edge at each node; see http://math.stackexchange.com/questions/264698.
import java.math.BigInteger;
public class Question264698 {
final static int max = 10000;
final static BigInteger quotientFactor = BigInteger.valueOf ((1L << 63) - 1);
public static double quotient (BigInteger num,BigInteger den) {
return num.multiply (quotientFactor).divide (den).doubleValue () / quotientFactor.doubleValue ();
}
@joriki
joriki / Question273348.java
Last active December 10, 2015 20:58
Count the number of distinct circles through at least three grid points in a square grid; see http://math.stackexchange.com/questions/273348.
import java.util.HashSet;
import java.util.Set;
public class Question273348 {
public static long gcd (long p,long q) {
if (p < 0)
p = -p;
if (q < 0)
q = -q;
@joriki
joriki / Question279924.java
Created January 16, 2013 11:00
Estimate the expected number of uniformly randomly selected derangements of non-fixed points required to sort a uniformly randomly selected permutation of n items; see http://math.stackexchange.com/questions/279924.
import java.util.Arrays;
import java.util.Locale;
import java.util.Random;
public class Question279924 {
final static int ntrials = 100000;
static Random random = new Random (0);
static void makeRandomPermutation (int [] p,int n) {
@joriki
joriki / Question280648.java
Created January 17, 2013 22:49
Estimate the probability that some subset of k out of n points uniformly randomly selected in the unit square forms a convex polygon; see http://math.stackexchange.com/questions/280648.
import java.util.Random;
public class Question280648 {
final static int ntrials = 1000000;
final static Random random = new Random ();
static class CombinationTraversal {
int k,n;
int [] combination;
@joriki
joriki / Question281094.java
Last active December 11, 2015 06:48
Search for permutations of objects in a circle for which any pair of objects is at a different minimal distance along the circle than originally; see http://math.stackexchange.com/questions/281094.
public class Question281094 {
static int m;
static int [] p;
static int count;
public static void main (String [] args) {
for (m = 2;;m++) {
p = new int [m];
p [0] = 1;
count = 0;
@joriki
joriki / Question280793.java
Created January 19, 2013 07:46
Estimate the expected value and variance of a transitivity ratio in a random graph; see http://math.stackexchange.com/questions/280793.
import java.util.Random;
public class Question280793 {
final static int n = 100;
final static int M = 150;
final static int N = (n * (n - 1)) / 2;
final static Random random = new Random ();
@joriki
joriki / Question281993.java
Created January 19, 2013 14:08
Estimate the probability of two points uniformly randomly chosen in a square or circle to lie on the same side of the line joining two other points thus chosen; see http://math.stackexchange.com/questions/281993.
public class Question281993 {
final static boolean square = false;
final static int ntrials = 10000000;
public static void main (String [] args) {
int count = 0;
double [] [] x = new double [4] [2];
for (int n = 0;n < ntrials;n++) {
for (int i = 0;i < 4;i++)
for (;;) {