Skip to content

Instantly share code, notes, and snippets.

@joriki
joriki / Binomials.java
Created March 29, 2020 14:25
Calculate factorials and binomial coefficients
import java.math.BigInteger;
public class Binomials {
static BigInteger [] [] binomials = new BigInteger [0] [0];
public static BigInteger binomial (int n,int k) {
if (k < 0 || n < 0)
throw new UnsupportedOperationException ();
if (k > n)
return BigInteger.ZERO;
@joriki
joriki / Question3600102.java
Created March 29, 2020 12:43
Count the 14-card hands drawn from a standard 52-card deck that contain at least one full house; see https://math.stackexchange.com/questions/3600102.
import java.math.BigInteger;
import java.util.function.Consumer;
public class Question3600102 {
static BigInteger count = BigInteger.ZERO;
static BigInteger [] multiplicities = new BigInteger [5];
static {
for (int i = 0;i <= 4;i++)
multiplicities [i] = Binomials.binomial(4, i);
}
@joriki
joriki / Question3597830.java
Created March 27, 2020 21:09
Count the 14-card hands drawn from a standard 52-card deck that contain at least two pairs of consecutive ranks; see https://math.stackexchange.com/questions/3597830.
import java.math.BigInteger;
import java.util.function.Consumer;
public class Question3597830 {
static BigInteger count = BigInteger.ZERO;
static BigInteger [] multiplicities = new BigInteger [5];
static {
for (int i = 0;i <= 4;i++)
multiplicities [i] = Binomials.binomial(4, i);
}
public class Question3594010 {
public static void main(String [] args) {
for (int [] p : Permutations.getPermutations (9)) {
int n1 = getNumber(p,0);
int n2 = getNumber(p,1);
if (n1 - n2 == 33333)
System.out.println(n1 + " - " + n2 + " = 33333");
}
}
@joriki
joriki / Question3588391.java
Created March 21, 2020 14:49
Compute the number of equivalence classes of 2-colorings of the edges of the n-dimensional hypercube under rotations and reflections (see https://math.stackexchange.com/questions/3588391).
import java.math.BigInteger;
import java.util.HashSet;
import java.util.Set;
public class Question3588391 {
public static void main(String [] args) {
for (int n = 1;;n++) {
int nvertices = 1 << n;
Set<Long> edges = new HashSet<>();
for (long direction = 0,edge = 1;direction < n;direction++,edge <<= 1)
@joriki
joriki / Question3578365.java
Created March 12, 2020 10:04
Find pandigital polydivisible numbers; see https://math.stackexchange.com/questions/3578365.
import java.math.BigInteger;
public class Question3578365 {
static int count;
static BigInteger result;
public static void main(String [] args) {
for (int n = 2;;n += 2) {
count = 0;
recurse (n,BigInteger.ZERO,0,new boolean [n]);
@joriki
joriki / Question3576721.java
Last active March 12, 2020 16:11
Find the empirical distribution of a ratio in a randomized Fibonacci-like recurrence and its geometric mean; see https://math.stackexchange.com/questions/3576721.
import java.util.Random;
public class Question3576721 {
final static long ntrials = 10000000000L;
final static int nbins = 1000;
final static int nmemories = 2;
final static int nstates = 1 << nmemories;
final static double minlog = -1.5;
final static double maxlog = 1.5;
final static Random random = new Random();
@joriki
joriki / Question3561819.java
Created March 10, 2020 22:32
Count the five-card hands dealt from a 52-card deck that consist of three face cards, a king and a spade (see https://math.stackexchange.com/questions/3561819).
public class Question3561819 {
public static void main(String [] args) {
int count = 0;
int [] hand = new int [5];
for (hand [0] = 0;hand [0] < 52;hand [0]++)
for (hand [1] = hand [0] + 1;hand [1] < 52;hand [1]++)
for (hand [2] = hand [1] + 1;hand [2] < 52;hand [2]++)
for (hand [3] = hand [2] + 1;hand [3] < 52;hand [3]++)
@joriki
joriki / Question3570779.java
Created March 8, 2020 11:57
Count the number of different admissible score sets in Dungeons and Dragons 3.5 edition; see https://math.stackexchange.com/questions/3570779.
public class Question3570779 {
final static int nscores = 6;
final static int min = 3;
final static int max = 18;
static int count;
public static void main(String [] args) {
recurse (new int [nscores],0);
@joriki
joriki / Question3535217.java
Created February 5, 2020 13:08
Count the ways of expressing an n-cycle as a product of n - 1 transpositions; see https://math.stackexchange.com/questions/3535217.
import java.util.ArrayList;
import java.util.List;
public class Question3535217 {
static long count;
static List<int []> transpositions = new ArrayList<>();
static int [] p;
public static void main(String [] args) {
for (int n = 1;;n++) {