Skip to content

Instantly share code, notes, and snippets.

@joriki
joriki / BigRational.java
Created April 13, 2020 15:28
Big rationals, much like big integers.
import java.math.BigInteger;
public class BigRational implements Comparable<BigRational> {
public static BigRational ZERO = new BigRational (0);
public static BigRational ONE = new BigRational (1);
BigInteger num;
BigInteger den;
public BigRational(long n) {
@joriki
joriki / Question3620950.java
Created April 12, 2020 01:13
Calculate abc-conjecture qualities for c,a power of three and two, respectively; see https://math.stackexchange.com/questions/3620950.
import java.io.IOException;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Locale;
public class Question3620950 {
final static long n = 0x80000000L;
public static void main (String [] args) {
boolean [] prime = new boolean [(int) (n >> 1)]; // prime [i] : is 2i + 1 prime?
public class Question3617128 {
static long count;
public static void main(String [] args) {
for (int i = 1,n = 1;;i++,n += i) {
count = 0;
recurse (new boolean [n],new int [n],new int [i + 1],0);
System.out.println(n + " : " + count);
}
}
@joriki
joriki / Question3609799.java
Created April 5, 2020 07:50
Find possible factors from two elements of a shuffle product; see https://math.stackexchange.com/questions/3609799.
import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class Question3609799 {
final static int k = 20;
final static int l = 30;
final static int n = k + l;
@joriki
joriki / Question2257188.java
Created April 2, 2020 15:46
Compute the variance of a random variable in a coin experiment; see https://math.stackexchange.com/questions/2257188.
public class Question2257188 {
final static int n = 4;
public static void main(String [] args) {
double s = 0;
double s2 = 0;
int [] [] permutations = Permutations.getPermutations(n);
for (int [] p : permutations) {
int sum = p [0] + 1;
for (int i = 1;i < p.length;i++)
@joriki
joriki / Question3606037.java
Created April 2, 2020 10:50
Simulate the expected average of dice rolls if the first roll is 1 and the trial ends when a roll repeats; see https://math.stackexchange.com/questions/3606037.
import java.util.Random;
public class Question3606037 {
final static long ntrials = 100000000;
final static Random random = new Random();
public static void main(String [] args) {
double total = 0;
for (long n = 0;n < ntrials;n++) {
double sum = 1;
@joriki
joriki / Question3605591.java
Created April 2, 2020 05:36
Apply various methods of apportioning seats in a parliament; see https://math.stackexchange.com/questions/3605591.
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
@joriki
joriki / Question3602087a.java
Created March 31, 2020 12:02
Calculate the exaxt probability distribution for the number of pairs of consecutive cards with matching suits; see https://math.stackexchange.com/questions/3602087.
package temp;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Question3602087a {
final static int NSUITS = 4;
final static int NRANKS = 13;
@joriki
joriki / Question3602087
Created March 31, 2020 06:39
Simulate the expected number of suit matches in pairs of consecutive cards in a standard 52-card deck; see https://math.stackexchange.com/questions/3602087.
import java.util.Random;
public class Question3602087 {
final static long ntrials = 10000000;
final static Random random = new Random();
final static int NSUITS = 4;
final static int NRANKS = 13;
final static int NCARDS = NRANKS * NSUITS;
@joriki
joriki / BallsInBins.java
Created March 29, 2020 14:27
Generate all configurations of balls in bins, optionally with limited capacity
import java.util.function.Consumer;
public class BallsInBins {
public static void traverseConfigurations (int nballs,int nbins,Consumer<int []> consumer) {
traverseConfigurations(nballs,nbins,Integer.MAX_VALUE,consumer);
}
public static void traverseConfigurations (int nballs,int nbins,int capacity,Consumer<int []> consumer) {
recurse (new int [nbins],nballs,capacity,0,consumer);
}