Skip to content

Instantly share code, notes, and snippets.

@joriki
joriki / Question769362.java
Created July 2, 2015 07:07
Calculate the number of permutation recipes that have a given effect (see http://math.stackexchange.com/questions/769362)
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
public class Question769362 {
final static String from = "cat";
final static String to = "abc";
final static int n = 26;
public class Question1347966 {
final static int nmoves = 6;
final static int horizon = 100;
public static void main (String [] args) {
// second innings
Rational [] r = new Rational [horizon + nmoves];
Rational [] value1 = new Rational [horizon];
Rational [] [] p1 = new Rational [horizon] [nmoves];
@joriki
joriki / Question1341400.java
Last active August 29, 2015 14:24
Efficiently test weighing schedules for a strange money weighing device; see http://math.stackexchange.com/questions/1341400
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import algebra.Matrix;
public class Question1341400 {
static List<Integer> getEquivalenceClasses (int n) {
List<Integer> result = new ArrayList<Integer> ();
@joriki
joriki / Question1351246.java
Last active August 29, 2015 14:24
How many games of N vs. N are required until all pairs of 2N players have played on the same and on different teams? See http://math.stackexchange.com/questions/1351246
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Question1351246 {
public static void main (String [] args) {
outer:
for (int n = 2;;n++) {
int n2 = n + n;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Question1357327 {
public static void main (String [] args) {
for (long s = 2;;s++)
try {
List<Long> factors = factor (s);
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Question1357327 {
final static int n = 0x40000000; // highest number to test
static boolean [] prime = new boolean [n / 2]; // prime [i] : is 2i + 1 prime?
public static void main (String [] args) {
@joriki
joriki / Question1341929.java
Created July 12, 2015 06:56
Try to solve a game of consolidating numbers; see http://math.stackexchange.com/questions/1341929
import java.util.Arrays;
public class Question1341929 {
public static void main (String [] args) {
for (int n = 2;;n++) {
int nslots = 2 * n - 1;
int [] p = new int [nslots];
int max = 0;
int [] best = null;
for (int bits = (1 << (nslots - 2));bits < 1 << (nslots - 1);bits += 2) {
@joriki
joriki / Question1341929Dynamic.java
Created July 12, 2015 17:12
Some more progress in proving the optimality of the results for a number game; see http://math.stackexchange.com/questions/1341929
import java.math.BigInteger;
import java.util.HashSet;
import java.util.Set;
public class Question1341929Dynamic {
final static int maxn = 1000;
final static int maxslots = 2 * maxn + 1;
static class Record {
BigInteger left;
@joriki
joriki / Question1364438.java
Created July 17, 2015 17:14
Calculate the number of permutations or derangements with given maximal cycle length; see http://math.stackexchange.com/questions/1364438
import java.math.BigInteger;
public class Question1364438 {
final static int PERMUTATIONS = 0;
final static int DERANGEMENTS = 1;
final static int n = 630;
static {
init (n);
@joriki
joriki / Question1013798.java
Created July 19, 2015 09:59
Find the maximal numbers of 3-cycles and 4-cycles in a tournament; see http://math.stackexchange.com/questions/1013798.
public class Question1013798 {
final static int cycleSize = 3;
static int mask (int i,int j) {
return 1 << shift (i,j);
}
// i > j
static int shift (int i,int j) {
return (i * (i - 1)) / 2 + j;