Skip to content

Instantly share code, notes, and snippets.

@joriki
joriki / Question201994.java
Created September 25, 2012 06:24
Find the maximal size of a subset of Z_3^3 without a line; see http://math.stackexchange.com/questions/201994/center-of-mass-of-triangle
import java.util.List;
import java.util.ArrayList;
public class Question201994 {
public static void main (String [] args) {
List<Integer> masks = new ArrayList<Integer> ();
for (int bits = 0;bits < 1 << 27;bits++) {
int count = 0;
int r1 = 0;
int r2 = 0;
@joriki
joriki / Question202862.java
Created September 26, 2012 20:57
Compute the fraction of combinations of Set cards that contain a Set; see http://math.stackexchange.com/questions/202862
public class Question202862 {
final static int nfeatures = 4;
final static int nvalues = 3;
final static int ncards = (int) Math.pow (nvalues,nfeatures);
static int thirdFeature (int f1,int f2) {
return f1 == f2 ? f1 : 3 - (f1 + f2);
}
static int thirdCard (int c1,int c2) {
@joriki
joriki / Question188889.java
Created October 11, 2012 19:49
Simulation of the expiring coupon collector's problem; see http://math.stackexchange.com/questions/188889
import java.util.Random;
import java.util.Arrays;
import java.util.Locale;
public class Question188889 {
static Random random = new Random ();
static int [] coupons;
static int [] counts;
static int couponCount;
@joriki
joriki / Question211687.java
Created October 12, 2012 21:09
Calculation and simulation of the probability of a relatively uniform draw; see http://math.stackexchange.com/questions/211687
import java.math.BigInteger;
import java.util.Random;
public class Question211687 {
final static int n = 100;
final static int m = 10;
final static int perKind = 100;
final static int min = 8;
final static int max = 12;
@joriki
joriki / Question218234.java
Created October 23, 2012 18:32
Simulate an M/M/1 queue and calculate moments of the distribution of total running times of k jobs encountered by an arriving job; see http://math.stackexchange.com/questions/218234
import java.util.Random;
public class Question218234 {
final static Random random = new Random ();
static double exponential (double mu) {
return -Math.log (random.nextDouble ()) / mu;
}
final static double lambda = 2;
@joriki
joriki / Question219552.java
Created October 24, 2012 09:14
Count the number of binary sequences with repeating initial segment; see http://math.stackexchange.com/questions/219552
public class Question219552 {
public static void main (String [] args) {
for (int n = 2;;n += 2) {
int n2 = n >> 1;
long count = 0;
for (long bits = 0;bits < 1L << n;bits++) {
outer:
for (int l = 1;l <= n2;l++) {
for (int bit = 0;bit < l;bit++)
@joriki
joriki / Question216503.java
Created October 26, 2012 04:34
Check whether Bob can force a zero pattern against Alice in a game of determinants; see
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
public class Question216503 {
final static int n = 5;
final static int [] weights = {0,1,n+1};
static class State {
@joriki
joriki / Question227990.java
Created November 3, 2012 12:05
On a randomly uniformly segmented circle, find the measure of the circle lying in segments that are smaller than at least one of the adjacent segments; see http://math.stackexchange.com/questions/227990
import java.util.Arrays;
public class Question227990 {
final static int nbirds = 5;
final static int ntrials = 10000000;
static double [] birds = new double [nbirds];
static double length (int index) {
double length = birds [(index + nbirds) % nbirds] - birds [(index + nbirds - 1) % nbirds];
return length < 0 ? length + 1 : length;
@joriki
joriki / Question231199.java
Created November 9, 2012 21:16
Determine the average and median time of all possible schedules for a bridge/river crossing problem; see http://math.stackexchange.com/questions/231199.
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class Question231199 {
final static double [] times = {1,2,5,10};
static List<Double> results = new ArrayList<Double> ();
static Set<Integer> west = new HashSet<Integer> ();
import java.util.Random;
public class Question245122 {
final static Random random = new Random ();
public static void main (String [] args) {
int N = Integer.parseInt (args [0]);
int ntrials = Integer.parseInt (args [1]);
int count = 0;